Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中设置请求标头(Set a request header in JavaScript)

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中设置RefererUser-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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

W3C Spec on setrequestheader .

(setrequestheader上的W3C规范 。)

The brief points:

(简要要点:)

If the request header had already been set, then the new value MUST be concatenated to the existing value using a U+002C COMMA followed by a U+0020 SPACE for separation.

(如果已经设置了请求报头,则必须使用U + 002C COMMA和U + 0020 SPACE将新值连接到现有值,以进行分离。)

UAs MAY give the User-Agent header an initial value, but MUST allow authors to append values to it.

(UA可以给User-Agent头一个初始值,但是必须允许作者在其后附加值。)

However - After searching through the framework XHR in jQuery they don't allow you to change the User-Agent or Referer headers.

(但是-在jQuery中搜索XHR框架后,它们不允许您更改User-Agent或Referer标头。)

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:

(我倾向于FF中的安全策略拒绝您要执行的操作-如果您想传递一些自定义Referer类型标头,则可以始终执行以下操作:)

xhr.setRequestHeader('X-Alt-Referer', 'http://www.google.com');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...