简体   繁体   English

跨域Ajax请求成功事件不会触发

[英]Cross domain ajax request success event doesn't fire

Here is the scenario which is surprisingly simple: 这是一个非常简单的场景:

I send an ajax request which targets a different domain: 我发送了针对另一个域的ajax请求:

$(function(){
        $.ajax({
            url: "http://www.drivescheduler.com/functions.php?function=getHighSchools&code=" + $("#code").val(),
            type: 'get',
            async: true,
            crossDomain: true,
            beforeSend: function(xhr){
                xhr.withCredentials = true;
            },
            success: function(x, status, xhr){
                alert();
            }
        });
    });

On the domain which receives the ajax request, I've properly configured CORS headers: 在接收ajax请求的域上,我已经正确配置了CORS标头:

header('Access-Control-Allow-Origin: http://temp.jfdin.net');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-MD5, X-Alt-Referer');
header('Access-Control-Allow-Credentials: true');
header("Content-Type: application/json; charset=utf-8");

And I can see the request being sent and the proper response being received through Chrome Debug Tools, but I get no alert() . 而且我可以通过Chrome调试工具看到发送的请求和正确的响应,但是没有得到alert()
In other words, the success event never gets fired. 换句话说,成功事件永远不会被触发。
Why is that and how can I fix it? 为什么会这样,我该如何解决?

The URL you are requesting responds with a JSON content type but an HTML body. 您请求的URL响应为JSON内容类型,但为HTML正文。

jQuery will fail to parse the HTML as JSON and error. jQuery将无法将HTML解析为JSON和错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM