无法将POST请求的Authorization标头设置为跨域

时间:2016-07-27 13:06:01

标签: javascript ajax cors authorization

我在向Java编写的api发送带有POST请求的Authorization标头时遇到问题,我将我的代码附加到响应和请求标头。

$.ajax({
    url: 'http://' + URLDomain + ':9090/path/to/api',
    type : 'POST',
    beforeSend: function (xhr) {             
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
       xhr.setRequestHeader("Authorization", encryptedText);
        },
    data: 'contentKey='+dataToSend,
     success: function(resp){
         console.log(resp);
     },
     error: function (err){
         console.log(err);
     }
});

请求标题: Accept:*/* Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8 Access-Control-Request-Headers:accept, access-control-allow-headers, access-control-allow-origin, authorization, content-type Access-Control-Request-Method:POST Cache-Control:no-cache Connection:keep-alive Host:xxx.xxx.x.xx:9090 Origin:http://xxx.xxx.x.xxx Pragma:no-cache Referer:http://xxx.xxx.x.xxx/Project/path/Web/ User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36

响应标题: Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, authorization Access-Control-Allow-Methods:POST, GET, HEAD, OPTIONS Access-Control-Allow-Origin:http://xxx.xxx.x.xxx Content-Length:0 Date:Wed, 27 Jul 2016 12:42:09 GMT Set-Cookie:JSESSIONID=xk1ehwk88fn1tk8k6f81sojx;Path=/;HttpOnly

我已关注以下链接: How to make GET CORS request with authorization headerHow to add CORS support on the server side in Java with Jersey 但问题仍未解决。

0 个答案:

没有答案