[英]Cross-domain AJAX request error on HTTP 200
I'm writing a very basic Facebook app, but I'm encountering an issue with cross-domain AJAX requests (using jQuery). 我正在编写一个非常基本的Facebook应用程序,但是遇到了跨域AJAX请求(使用jQuery)的问题。
I've written a proxy page to make requests to the graph via cURL that I'm calling via AJAX. 我编写了一个代理页面,以通过我通过AJAX调用的cURL向图形发出请求。 I can visit the page in the browser and see it has the correct output, but requesting the page via always causes jQuery to fire the error handler callback.
我可以在浏览器中访问该页面,并看到它具有正确的输出,但是通过始终请求页面会导致jQuery触发错误处理程序回调。
So I have two files: 所以我有两个文件:
Proxy, which does the cURL request 代理,执行cURL请求
<?php
//Do some cURL requests, manipulate some data
//return it as JSON
print json_encode($data);
?>
The facebook canvas, which contains this AJAX call facebook画布,其中包含此AJAX调用
$.getJSON("http://myDomain.com/proxy.php?get=stuff",
function(JSON)
{
alert("success");
})
.error(function(err)
{
alert("err");
});
Inspecting the call with Firebug shows it returns with HTTP code 200 OK
, but the error handler is always fired, and no content is returned. 使用Firebug检查该调用会显示其返回的HTTP代码为
200 OK
,但始终会触发错误处理程序,并且不会返回任何内容。 This happens whether I set Content-Type: application/json
or not . 无论我是否设置
Content-Type: application/json
发生这种情况 。
I have written JSON-returning APIs in PHP before using AJAX and never had this trouble. 在使用AJAX之前,我已经用PHP编写了返回JSON的API,并且从未遇到过这种麻烦。
What could be causing the request to always trigger the error handler? 是什么导致请求始终触发错误处理程序?
Recently I experienced the same issue and my problem was the fact that there was a domain difference between the webpage and the API, due to the SSL. 最近,我遇到了同样的问题,我的问题是由于SSL,网页和API之间存在域差异。
The web page got a HTTP address (http://myDomain.com) and the content I was requesting with JQuery was on the same domain but HTTPS protocol (https://myDomain.com). 网页上有一个HTTP地址(http://myDomain.com),而我向JQuery请求的内容位于同一域,但是HTTPS协议(https://myDomain.com)。 The browser (Chrome in this case) considered that the domains were differents (the first one with HTTP , the second one with HTTPS ), just because of the protocol, and because the request response type was "application/json", the browser did not allowed it.
浏览器(在本例中为Chrome)认为域是不同的(第一个域使用HTTP ,第二个域使用HTTPS ),只是由于协议的缘故,并且因为请求响应类型为“ application / json”,所以浏览器做了不允许。
Basically, the request worked fine, but your browser did not allowed the response content. 基本上,该请求可以正常工作,但您的浏览器不允许响应内容。
I had to add a " Access-Control-Allow-Origin " header to make it work. 我必须添加一个“ Access-Control-Allow-Origin ”标题才能使其工作。 If you're in the same case, have a look there: https://developer.mozilla.org/en/http_access_control .
如果您是同一情况,请在此处查看: https : //developer.mozilla.org/en/http_access_control 。
I hope that'll help you, I got a headache myself. 希望对您有帮助,我自己很头疼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.