斗士狗 2014-02-01 01:13 采纳率: 0%
浏览 85

CORS和jQuery ajax调用

I'm not sure if it's simply a case of an incorrect setting in my ajax call or my misunderstanding how CORS works.

I have a web server running on my box in the network-- larryq.mycompany.com. I have full access to that machine and have been setting the following headers on my pages:

Access-Control-Allow-Origin = *
Access-Control-Allow-Headers = 'Authorization'
Access-Control-Allow-Credentials = true
Access-Control-Allow-Methods = "GET, POST, PUT, DELETE"

When I load my current testing page I see those values in my response headers.

On this page I'm making an ajax call to another server on the network, using OAuth:

var OAuthAuthorizationString = 'OAuth realm="http://www.mycompany.com/", oauth_consumer_key="consumerkey"....oauth_nonce="1446691", oauth_version="1.0"'

$.ajax
({
    type: 'GET',
    url: 'https://secure.mycompany.com?val1=33&val2=45',
    data: [], 
    beforeSend: function (xhr) {
        xhr.setRequestHeader('Authorization', 'OAuthAuthorizationString');
    }
})
.done(function (html) {
    $("#results").empty();
    $("#results").append(html);
})
.fail(function (jqXHR, textStatus) {
    $("#results").empty();
    $("#results").append(textStatus);
});

Right now when I run this I get a 403 Forbidden response during what appears to be the CORS preflight check.

If I open the Google Advanced Rest Client and craft the same GET request using that OAuth string in the Authorization header, the request works and I get back my data.

I must be doing something wrong, but can't figure what? Am I running into some ajax restriction I'm not aware of or (quite likely) not setting things up correctly?

I'm using jQuery 1.10 fwiw.

  • 写回答

1条回答 默认 最新

  • weixin_33698043 2014-07-11 09:11
    关注

    I am trying to understand how to use CORS myself, so this may not solve your problem, but according to the documentation:

    Important note: when responding to a credentialed request, server must specify a domain, and cannot use wild carding.

    So you cannot have:

    Access-Control-Allow-Origin = *
    Access-Control-Allow-Credentials = true
    

    Instead, you must explicitly specify the allowed origin. For example:

    Access-Control-Allow-Origin = larryq.mycompany.com
    Access-Control-Allow-Credentials = true
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?