我们如何使用AJAX发送/获取http头信息?
问题描述:
有没有什么办法通过AJAX发送/获取http头(比如content-type ...)?然后,可以请解释我,我们将通过在AJAX中传递http标题来存档什么,以及将使用这种技术?我们如何使用AJAX发送/获取http头信息?
感谢
答
我不是专家,
但是你应该看看AJAX对象XmlHttpHeader和the 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();
}
感谢@stephane。我已经知道XmlHttpHeader。但是,我需要非常简短的信息和关于这个问题的例子? – 2011-03-31 13:50:23
我认为[www.w36.org参考文献] [1]的介绍非常简短,不是吗? [1]:http://www.w3.org/TR/XMLHttpRequest/#introduction – 2011-03-31 13:58:35