开发者

Ajax jquery unauthorized request

I am mak开发者_如何学Cing an ajax request using Jquery and everything works fine except the page is protected so only logged in users can make request. I am already logged in but I think Jquery ajax doesn't send my cookies to the url. How can I solve this error so cookies, headers are also sent so the page doesn't treat it as 401 request?

Thanks in advance :)


It sounds like you're trying to request a page on a different domain from the one your site is running on. If this is the case then it's the browser that is telling you the request is unauthorized rather than the other site.

Edit:

If this is the case and you control the other website then I believe you can allow requests using a specific header (http://www.w3.org/TR/2008/WD-access-control-20080912/#access-control-allow-origin). If you don't control the other server then there is no (pure javascript) way round this problem.

You could run a proxy on a server you control to request the page and pass it on to you. However this will still have the problem that it will be requesting the page without your user's cookies.


my jquery solution: in your ajax call(for basic authorization):

headers: {
                "Authorization": "Basic " + btoa('username:password')
},

or

beforeSend: function (xhr){ 
                xhr.setRequestHeader('Authorization', "Basic " + btoa('username:password')); 
},


You still can check session as normal

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜