【问题标题】:jquery ajax returns status code 0 while requesting cross domainjquery ajax在请求跨域时返回状态码0
【发布时间】:2013-05-22 17:57:55
【问题描述】:

我正在尝试进行跨域请求,但服务器无法响应

Access-Control-Allow-Origin: *

我正在尝试读取响应标头。 当我尝试读取状态码或就绪状态时,它返回 0。我的 ajax 请求代码如下。

$.ajax({
       url: "https://accounts.google.com/o/oauth2/auth",
       type: "POST",
        data:   {"response_type":"token","client_id":"6720XXXXXXX.apps.googleusercontent.com","redirect_uri":"http://localhost:51967/oauth2callback.aspx","scope":"https://www.googleapis.com/auth/calendar","state":"this is state information","approval_prompt":"auto"},            
                crossDomain: true
        }).always(function(jqxhr,testStatus,errorThrown)
        {
                alert(jqxhr.readyState);
                alert(jqxhr.status);
        });

提前致谢

【问题讨论】:

    标签: javascript html ajax jquery


    【解决方案1】:

    你试过this jquery crossdomain 插件吗?

    另一种方法是使用jsonp 作为响应类型

    $.ajax({
       url: "https://accounts.google.com/o/oauth2/auth",
       type: "POST",
       dataType: 'jsonp',
       ...
    

    【讨论】:

    • 那么服务器将返回 400 错误,因为它在使用 jsonp 时只接受某些数据作为 post 发送其他数据,例如 callback=jQuery19009566406458362526_13611668XXX
    猜你喜欢
    • 2014-09-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2012-04-20
    • 2011-08-10
    • 2011-07-03
    相关资源
    最近更新 更多