有什么方法可以通过 AJAX 发送/获取 http header (例如,content-type...)?那么,请解释一下,我们将通过在 AJAX 中传递 http header 来存档什么以及将在何处使用此技术?
谢谢
最佳答案
我不是专家,
但是你应该看看 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();
}
关于ruby-on-rails - 我们如何使用 AJAX 发送/获取 http header 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5500514/