跨域JavaScript的Ajax请求 - 状态200确定的,但没有任何反应 [英] Cross domain javascript ajax request - status 200 OK but no response

查看:375
本文介绍了跨域JavaScript的Ajax请求 - 状态200确定的,但没有任何反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的情况: 即时创建插件该网站管理员可以在其网站嵌入的数据存储在我的服务器。所以剧本基本上已经做出一个Ajax请求的PHP文件在我的服务器来更新数据库。对?对 :) Ajax请求的做工精良当我运行它在我的本地服务器,但它确实的不工作时,PHP文件在的我的在线的服务器。 这是一个用code IM:

Here is my situation: Im creating a widget that site admins can embed in their site and the data are stored in my server. So the script basically has to make an ajax request to a php file in my server to update the database. Right? Right :) The ajax request works excellent when i run it in my local server but it does not work when the php file is on my ONLINE server. This is the code im using:

var url = "http://www.mydomain.net/ajax_php.php";
var params = "com=ins&id=1&mail=mymail@site.net";
http.async = true;
http.open("POST", url, true);       

http.onreadystatechange = function() {

    if(http.readyState == 4 && http.status == 200) {

    //do my things here
    alert( http.responseText ); 

    }
}
http.send(params);

在萤火虫它表明: http://www.mydomain.net/ajax_php.php 200行X 600毫秒。

In firebug it shows: http://www.mydomain.net/ajax_php.php 200 OK X 600ms.

当我检查了ajax responnseText我总是得到一个状态:0

When i check the ajax responnseText I always get a Status:0

现在我的问题是:我能在默认情况下跨域Ajax请求也许这是一个跨域Ajax的问题​​,因为它工作时所需的文件驻留在我的本地服务器,但在要求不工作?文件在另一台服务器,即时通讯思想Ajax请求到另一个远程服务器可能会被拒绝?你能帮我清楚吗? 谢谢..

Now my question is: "Can i do cross-domain ajax requests by default? Might this be a cross-domain ajax problem? Since it works when the requested file resides in my local server but DOESN'T work when the requested file is in another server, im thinking ajax requests to another remote server might be denied? Can you help me clear on this? Thanks..

推荐答案

跨域请求不允许直接。然而,有一个常用的技术,称为 JSONP ,可以让你避免这种限制通过使用脚本标记。基本上,你有一个响亮的名字创建一个回调函数:

Cross-domain requests are not directly allowed. However, there is a commonly-used technique called JSONP that will allow you to avoid this restriction through the use of script tags. Basically, you create a callback function with a known name:

function receiveData(data) {
    // ...
}

然后你的服务器包装JSON数据在函数调用,就像这样:

And then your server wraps JSON data in a function call, like this:

receiveData({"the": "data"});

和你呼通过添加剧本标签到您的网页跨域服务器。 jQuery的包装典雅,所有这一切都在它的 AJAX 的功能。

And you "call" the cross-domain server by adding a script tag to your page. jQuery elegantly wraps all of this up in its ajax function.

这是我曾在多次使用另一种方法是通过内置页框跨文档通信。您可以通过的postMessage <有一个窗口聊到另一个,甚至跨域,以限制的方式/ A>。请注意,只有最新的浏览器有这个功能,这样的选择是不可行的在任何情况下不诉诸两轮牛车。

Another technique that I've had to use at times is cross-document communication through iframes. You can have one window talk to another, even cross-domain, in a restricted manner through postMessage. Note that only recent browsers have this functionality, so that option is not viable in all cases without resorting to hackery.

这篇关于跨域JavaScript的Ajax请求 - 状态200确定的,但没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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