即使状态回来是200,跨域ajax也会遇到错误

时间:2014-12-20 16:15:46

标签: ajax cross-domain

我发送跨域ajax请求,响应返回状态200.我也看到请求到达服务器。

我的服务器中有这个:

context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
context.Response.Headers.Add("Access-Control-Allow-Headers", "*");

这是在客户端:

$.ajax({
        type: "POST",
        url: this.SERVER + url,
        data: data,
        xhrFields: {
            withCredentials: true
        },
        success: function (a, b) {
            debugger;
            alert("sdsd");
        },error : function(a,b) {
            debugger;
        },
        dataType: 'json'
    });

这是Chrome浏览器的请求

enter image description here

在Firefox中我得到错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:2000/PageHandler.ashx. This can be fixed by moving the resource to the same domain or enabling CORS

1 个答案:

答案 0 :(得分:0)

我的猜测是,由于您要发送Access-Control-Allow-Credentials标题,因此无法将*放入Access-Control-Allow-Origin。尝试将原点指定为JavaScript客户端的原点。

我基于CORS上的Mozilla开发人员网络文档中的以下信息:

  

origin参数指定可以访问资源的URI。   浏览器必须强制执行此操作。对于没有凭据的请求,   服务器可以将“*”指定为通配符,从而允许任何原点   访问资源。