Javascript 什么是“xmlhttp.setRequestHeader();” 在什么情况下使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8882627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is 'xmlhttp.setRequestHeader();' and in which situations is it used?
提问by oFca
I stumbled on this command while learning AJAX. The guy who made the tutorial didn't explain this command, what do the parameters inside the command mean and what is it used for... Below is the code I used it in:
我在学习 AJAX 时偶然发现了这个命令。做教程的那个人没有解释这个命令,命令里面的参数是什么意思,是干什么用的……下面是我用的代码:
<script type="text/javascript">
function insert(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
};
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('message').innerHTML = xmlhttp.responseText;
};
};
parameters = 'insert_text='+document.getElementById('insert_text').value;
xmlhttp.open('POST','ajax_posting_data.php',true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(parameters);
};
</script>
回答by hvgotcodes
HTTP is a protocol. Part of that protocol is the concept of request headers. When an xhr happens, text is exchanged between the client and server. Request headers are part of the text that the client sends to the server.
HTTP 是一种协议。该协议的一部分是请求标头的概念。当 xhr 发生时,文本在客户端和服务器之间交换。请求头是客户端发送到服务器的文本的一部分。
This is a way to set the request headers. The arguments you see are
这是一种设置请求标头的方法。你看到的论点是
1) the header to set (in this case, Content-type)
2) the header value. (in this case, x-www-form-urlencoded)
1) 要设置的标头(在本例中为Content-type)
2) 标头值。(在这种情况下,x-www-form-urlencoded)
回答by jAndy
It is exactly what it says. It will set a "header" information for the next XMLHttpRequest
.
这正是它所说的。它将为下一个XMLHttpRequest
.
A header is pretty much a key/value pair. It is used to transmit "meta" information to the target server for the ongoing request. In your particular instance, its used to tell the server which content type is used for this request.
标头几乎是一个键/值对。它用于为正在进行的请求向目标服务器传输“元”信息。在您的特定实例中,它用于告诉服务器此请求使用哪种内容类型。
回答by Trunk
HTTP requests are messages passed from one computer system to another according to a set routine (a 'protocol' - here HyperText Transfer Protocol) in order to do things like send data, ask for data to be sent back, update data previously sent, etc.
HTTP请求根据一组例行程序从一个计算机系统传递的消息到另一个(A“协议” -此处ħyper Ť分机Ť转让(BOT)Protocol)以便做的事情一样发送数据,请求要发送回数据,更新之前发送的数据等
A header is basically a piece of information about the data in the body of the HTTP request. Its purpose is to tell the machine receiving the request what type of data is enclosed in the body of the request, its formatting, the language used, if it's to set a cookie, the date, the host machine, etc.
标头基本上是有关 HTTP 请求正文中数据的一条信息。它的目的是告诉接收请求的机器在请求正文中包含什么类型的数据、格式、使用的语言、是否设置 cookie、日期、主机等。
More than one header can be put on a HTTP request and each header has a 'name' and a 'value' component. On web pages they look like
一个 HTTP 请求可以放置多个标头,每个标头都有一个“名称”和一个“值”组件。在网页上它们看起来像
<meta name="........" content="............."/>
and you find them just below the top of the web page within the element.
您可以在元素中网页顶部的下方找到它们。
To enable people to send HTTP requests from within a JavaScript function, we create a new XMLHttpRequest object, just as your code does so with
为了使人们能够从 JavaScript 函数内发送 HTTP 请求,我们创建了一个新的 XMLHttpRequest 对象,就像您的代码所做的那样
xmlhttp = new XMLHttpRequest();
To this new empty object you intend to add data. Despite its name, XMLHttpRequest also allows sending data in a number of formats other than XML, e.g. HTML code, text, JSON, etc. In your example each data name will be separated from its value by an "=" character and each data/value pairing will be separated from the next pairing by an "&" character. This kind of formatting is known as URL encoding.
您打算向这个新的空对象添加数据。尽管它的名字,XMLHttpRequest 还允许以除 XML 之外的多种格式发送数据,例如 HTML 代码、文本、JSON 等。在您的示例中,每个数据名称将与其值通过“=”字符和每个数据/值配对将通过“&”字符与下一个配对分开。这种格式称为 URL 编码。
We have to tell the receiving computer how the data within the HTTP request body is encoded. There is a standard headerto convey this and it is added to the request via the method setRequestHeader(..). This method uses 2 parameters, the header name and the header's value. All this operation is achieved in the line
我们必须告诉接收计算机 HTTP 请求正文中的数据是如何编码的。有一个标准头来传达这一点,它通过方法setRequestHeader(..)添加到请求中。此方法使用 2 个参数,标头名称和标头的值。所有这些操作都在行中实现
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
This setRequestHeader(..)method must be applied to the request afterthe request is characterized with the open(...)method but beforethe final request is sent off with the send(.)method.
此setRequestHeader(..)方法必须在使用open(...)方法对请求进行特征化之后但在使用send(.)方法发送最终请求之前应用于请求。
The open(...)method defines: (1) the type of HTTP request, e.g. GET/POST/PUT etc; (2) the web page that contains the handling script for this request, e.g. some .php file or Node.js request endpoint that makes the appropriate query to the back end database; and (3) the nature of the request dynamics, e.g. asynchronous requests are assigned a value 'true', synchronous requests are assigned 'false'.
在开放(...)方法定义:(1)HTTP请求的类型,例如,GET / POST / PUT等; (2) 包含此请求处理脚本的网页,例如一些 .php 文件或 Node.js 请求端点,用于对后端数据库进行适当的查询;(3) 请求动态的性质,例如异步请求被赋值为“true”,同步请求被赋值为“false”。
The send(.)method attaches the data to be sent within the body of the request, in your case the variable called 'parameters'.
的发送(。)方法附加数据到请求的主体内被发送,你的情况被称为“参数”的变量中。
On your broader question of which situations setRequestHeader(..)is used, I would say that it is used in most HTTP request situations. But some types of dataadded to the body of a HTTP request invoke a default setting for the 'Content-Type' header.
关于使用setRequestHeader(..)的更广泛问题,我会说它用于大多数 HTTP 请求情况。但是添加到 HTTP 请求正文的某些类型的数据会调用“Content-Type”标头的默认设置。
回答by JanL
It sets the Content-type HTTP header to contain url encoded data sent from a form.
它将 Content-type HTTP 标头设置为包含从表单发送的 url 编码数据。