jQuery的阿贾克斯函数返回一个错误 [英] jQuery ajax function returns an error

查看:174
本文介绍了jQuery的阿贾克斯函数返回一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说实话,我是一个总的初学者使用jQuery现在我被困。我想从我的HTML表单的PHP发送数据时,它会将数据传输到数据库,并返回了一些,我想显示对我原先的HTML值。这是我的code:

to be honest, I'm a total beginner with jQuery and now I'm stuck. I want to send data from my HTML form to a php, it adds the data to a database and returns some value that I'd like to display on my original HTML. Here's my code:

$.ajax({
  type: "POST",
  url: "http://mysite.com/process.php",
  data: { data: mydata },
  cache: false,
  dataType: "text",
  error: function(jqXHR, textStatus, errorThrown){
        alert(jqXHR.status);
        alert(jqXHR.statusText);
        alert(jqXHR.responseText);
    },
  success: function(data){
        getContentBox().innerHTML = data;
}
});

它返回状态 = 0,状态文本 =错误而空<$ C $一个jqXHR对象C>的responseText 。然而,我的PHP似乎工作,我看到了我的数据库插入我的数据。我究竟做错了什么?

It returns a jqXHR object with status=0, statusText="error" and empty responseText. However, my php seems to work, I see my data inserted in my DB. What am I doing wrong?

任何帮助将是AP preciated。 在此先感谢!

Any help would be appreciated. Thanks in advance!

编辑: 镀铬控制台说: XMLHtt prequest无法加载 http://mysite.com/data.php 。产地 http://www.mysite.com 不受访问控制 - 允许 - 产地允许的。

Chrome console says XMLHttpRequest cannot load http://mysite.com/data.php. Origin http://www.mysite.com is not allowed by Access-Control-Allow-Origin.

推荐答案

ShelbyZ的评论使我的解决方案:

ShelbyZ's comment led me to the solution:

浏览器拒绝执行请求时,我试图用一个绝对的URL,我不得不写为相对的。

The browser refused to execute the request when I tried using an absolute URL, i had to write it as relative.

$.ajax({
  type: "POST",
  url: "process.php",
  data: { data: mydata },
  cache: false,
  dataType: "text",
  error: function(jqXHR, textStatus, errorThrown){
        alert(jqXHR.status);
        alert(jqXHR.statusText);
        alert(jqXHR.responseText);
    },
  success: function(data){
        getContentBox().innerHTML = data;
}
});

谢谢!

这篇关于jQuery的阿贾克斯函数返回一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆