Custom headers with $.ajax type jsnop or json

荒凉一梦 提交于 2019-12-20 03:28:05

问题


I have a problem with sending some custom headers to with a jQuery ajax JSON (or JSONP) request.

Code looks like this:

$.ajax({
      beforeSend: function(xhr) {
        xhr.setRequestHeader("X-VER",       VER);
        xhr.setRequestHeader("X-TIMESTAMP", now);
     },
     type: 'GET',
     data: null,
     url: site_uri,
     dataType: 'jsonp',
     success: function(msg){$(selector).html(msg);}
});

Firebug shows no custom headers. In addition the url changes with jsonp (can I change this?)

Edit:

I found that it probably violates http://en.wikipedia.org/wiki/Same_origin_policy but have no idea how to get it fixed (the script can't be on the same domain).

Thanks for any help


回答1:


If you have server access, a solution would be HTTP access control modification. Otherwise, probably the best idea is some kind of reverse proxy solution, I have no idea how good of support access control has outside of Firefox.




回答2:


jsonp isn't XHR (you are simply adding script elements to the head), emulating XHR. Hence, you can't set custom headers.



来源:https://stackoverflow.com/questions/3759575/custom-headers-with-ajax-type-jsnop-or-json

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