我们如何使用AJAX发送/获取http标头信息?

时间:2011-03-31 13:20:26

标签: ruby-on-rails ajax http

有没有办法通过AJAX发送/获取http标头(例如,content-type ...)?然后,请解释一下,我们将通过传递AJAX中的http标头以及将使用此技术的位置进行存档?

由于

1 个答案:

答案 0 :(得分:1)

我不是专家,

但是你应该看一下AJAX对象XmlHttpHeaderthe wikipedia article here

编辑:引用www.w3.org参考:

function test(data) {
 // taking care of data
}

function handler() {
 if(this.readyState == 4 && this.status == 200) {
  // so far so good
  if(this.responseXML != null && this.responseXML.getElementById('test').firstChild.data)
     // success!
   test(this.responseXML.getElementById('test').firstChild.data);
  else
   test(null);
 } else if (this.readyState == 4 && this.status != 200) {
  // fetched the wrong page or network error...
  test(null);
 }
}

var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "unicorn.xml");
client.send();

如果您只想将消息记录到服务器:

function log(message) {
 var client = new XMLHttpRequest();
 client.open("POST", "/log");
 client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
 client.send(message);
}

或者,如果要检查服务器上文档的状态:

function fetchStatus(address) {
 var client = new XMLHttpRequest();
 client.onreadystatechange = function() {
  // in case of network errors this might not give reliable results
  if(this.readyState == 4)
   returnStatus(this.status);
 }
 client.open("HEAD", address);
 client.send();
}