setRequestHeader does not work in JSONP using jQuery

自古美人都是妖i 提交于 2019-11-29 13:19:54

As jfriend00 says,

A JSONP request does not use an actual ajax call where you can control the HTTP request.

However in your case - the header you're attempting to add being basic Authorization - you can just inject that into the URL, as you'd "usually" do. So to authenticate at http://example.com with the user "user" and password "pass", the URL would be http://user:pass@example.com/.

A JSONP request does not use an actual ajax call where you can control the http request.

Instead, it injects a script tag into your page and the browser then requests that script src file. The resulting script that is returned executes and communicates back it's result via that javascript execution (calling a callback function). Because the request is generated via a script tag insertion and not a direct ajax call, there is no ability to set request headers on a JSONP request.

If your request is cross domain, then JSONP is your only mechanism.

If your request is the same origin as the page (not cross domain), then you could restructure your request to be an actual ajax call that returns JSON data. If you did that, you could set request headers on the ajax call.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!