问题描述
看来我无法从JavaScript做一个AJAX调用使用XMLHtt prequest何时改变大多数请求头。请注意,当 request.setRequestHeader
已到后, request.open被称为()
Gecko浏览器(请参阅<一href="http://ajaxpatterns.org/Talk:XMLHtt$p$pquest_Call">http://ajaxpatterns.org/Talk:XMLHtt$p$pquest_Call).当我设置的引用站点的,它不会设置(我看着使用萤火虫和篡改数据发送的请求标头)。当我设置的的User-Agent 的,它搞砸了AJAX调用完全。设置的接受和内容类型的做工作,但是,难道我们从设置pvented $ P $的引用站点和用户代理的在Firefox 3?
VAR请求=新XMLHtt prequest();VAR路径=http://www.yahoo.com;request.onreadystatechange = state_change;request.open(GET,路径,真实);request.setRequestHeader(Referer的,http://www.google.com);//request.setRequestHeader("User-Agent,的Mozilla / 5.0);request.setRequestHeader(接受,text / plain的);request.setRequestHeader(内容类型,text / plain的);request.send(空); 功能state_change(){如果(request.readyState == 4) {// 4 =装 如果(request.status == 200) {// 200 = OK // ...我们code在这里... 警报('OK'); } 其他 { 警报(问题检索XML数据); } }}
的。
简要几点:
但是 - 通过框架XHR jQuery中,他们不允许您更改用户代理或引用站点标题搜索后。最接近的事:
//设置标题所以调用的脚本都知道,这是一个XMLHtt prequest
xhr.setRequestHeader(X-要求,用,XMLHtt prequest);
我倾向于认为你想要做的是什么在FF否认了安全策略的观点 - 如果你想通过一些自定义的引用站点
键入头你总是可以做:
xhr.setRequestHeader(X-ALT-Referer的','http://www.google.com');
It seems that I am unable to change most request headers from JavaScript when making an AJAX call using XMLHttpRequest. Note that when request.setRequestHeader
has to be called after request.open()
in Gecko browsers (see http://ajaxpatterns.org/Talk:XMLHttpRequest_Call). When I set the Referer, it doesn't get set (I looked at the request headers sent using Firebug and Tamper Data). When I set User-Agent, it messed up the AJAX call completely. Setting Accept and Content-Type does work, however. Are we prevented from setting Referer and User-Agent in Firefox 3?
var request = new XMLHttpRequest();
var path="http://www.yahoo.com";
request.onreadystatechange=state_change;
request.open("GET", path, true);
request.setRequestHeader("Referer", "http://www.google.com");
//request.setRequestHeader("User-Agent", "Mozilla/5.0");
request.setRequestHeader("Accept","text/plain");
request.setRequestHeader("Content-Type","text/plain");
request.send(null);
function state_change()
{
if (request.readyState==4)
{// 4 = "loaded"
if (request.status==200)
{// 200 = OK
// ...our code here...
alert('ok');
}
else
{
alert("Problem retrieving XML data");
}
}
}
The brief points:
However - After searching through the framework XHR in jQuery they don't allow you to change the User-Agent or Referer headers. The closest thing:
// Set header so the called script knows that it's an XMLHttpRequest
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
I'm leaning towards the opinion that what you want to do is being denied by a security policy in FF - if you want to pass some custom Referer
type header you could always do:
xhr.setRequestHeader('X-Alt-Referer', 'http://www.google.com');
这篇关于在JavaScript设置一个请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!