ruby-on-rails - 我们如何使用 AJAX 发送/获取 http header 信息?

标签 ruby-on-rails ajax http

有什么方法可以通过 AJAX 发送/获取 http header (例如,content-type...)?那么,请解释一下,我们将通过在 AJAX 中传递 http header 来存档什么以及将在何处使用此技术?

谢谢

最佳答案

我不是专家,

但是你应该看看 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();
}

关于ruby-on-rails - 我们如何使用 AJAX 发送/获取 http header 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5500514/

相关文章:

ruby-on-rails - OmniAuth invalid_response 与 Twitter

ruby-on-rails - 如何将 Byebug 与远程进程(例如 pow)一起使用

javascript - 由于 csrf 保护,codeigniter 3 403(禁止)中的 XMLHttpRequest() POST 调用

python - 是否应该在每个响应中设置 cookie header ?

php - 来自 Javascript 的 Ajax 调用的 Http 响应 0

css - 如果找到特定字符串,则有条件地添加一个类 Rails CoffeeScript

mysql - bundler : failed to load command: spring (/bin/spring)

javascript - 在 Asp.net 中使用 ajax 调用显示 google map

php - 使用ajax的文件输入codeigniter

http - Chromium 最大并发 HTTP 连接数