在使用XMLHttpRequest进行AJAX调用时,似乎无法从JavaScript更改大多数请求标头。请注意,request.setRequestHeader必须request.open()在Gecko浏览器中调用when (请参阅http://ajaxpatterns.org/Talk:XMLHttpRequest_Call)。设置Referer时,它没有设置(我查看了使用Firebug和Tamper Data发送的请求标头)。当我设置User-Agent时,它完全搞砸了AJAX调用。但是,设置接受和内容类型确实可以。我们是否无法在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");
}
}
}
相关分类