It seems that I am unable to change most request headers from JavaScript when making an AJAX call using XMLHttpRequest.
(在使用XMLHttpRequest进行AJAX调用时,似乎无法从JavaScript更改大多数请求标头。)
Note that when request.setRequestHeader
has to be called after request.open()
in Gecko browsers (see http://ajaxpatterns.org/Talk:XMLHttpRequest_Call ).(请注意,当request.setRequestHeader
具有之后被称为request.open()
Gecko浏览器(见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).(设置Referer时 ,它没有设置(我查看了使用Firebug和Tamper Data发送的请求标头)。)
When I set User-Agent , it messed up the AJAX call completely.(当我设置User-Agent时 ,它完全搞砸了AJAX调用。)
Setting Accept and Content-Type does work, however.(但是,设置接受和内容类型确实可以。)
Are we prevented from setting Referer and User-Agent in Firefox 3?(我们是否无法在Firefox 3中设置Referer和User-Agent ?)
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");
}
}
}
ask by user121196 translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…