什么是“xmlhttp.setRequestHeader();”以及在什么情况下使用它?

发布于 2024-12-27 04:48:42 字数 927 浏览 1 评论 0原文

我在学习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>

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:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆依然 2025-01-03 04:48:42

HTTP 是一种协议。该协议的一部分是请求标头的概念。当 xhr 发生时,客户端和服务器之间会交换文本。请求标头是客户端发送到服务器的文本的一部分。

这是设置请求标头的一种方法。您看到的参数是

1) 要设置的标头(在本例中为 Content-type
2) 标头值。 (在本例中为x-www-form-urlencoded

请参阅此内容以获取更多信息.

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.

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)

See this for more info.

注定孤独终老 2025-01-03 04:48:42

HTTP 请求是根据设定例程(“协议” - 这里HyperText T传输)从一个计算机系统传递到另一个计算机系统的消息协议),以便执行诸如发送数据、请求发回数据、更新先前发送的数据等操作。

标头基本上是有关协议正文中数据的一条信息。 HTTP 请求。它的目的是告诉接收请求的机器请求正文中包含什么类型的数据、其格式、使用的语言、是否设置 cookie、日期、主机等。

多个标头可以放在 HTTP 请求上,每个标头都有一个“名称”和一个“值”组件。在网页上,它们看起来像

<meta name="........" content="............."/>

,您可以在元素内的网页顶部下方找到它们。

所做的那样

const xmlhttp = new XMLHttpRequest();

为了使人们能够从 JavaScript 函数内发送 HTTP 请求,我们创建一个新的 XMLHttpRequest 对象,就像您的代码在您打算向这个新的空对象添加数据时 。尽管名称如此,XMLHttpRequest 还允许以 XML 以外的多种格式发送数据,例如 HTML 代码、文本、JSON 等。在您的示例中,每个数据名称与其值将通过“=”字符分隔,每个数据/值配对将通过“&”与下一个配对分隔开。特点。这种格式称为 URL 编码。

我们必须告诉接收计算机 HTTP 请求正文中的数据是如何编码的。有一个 标准标头 来传达这一点,并通过该方法将其添加到请求中setRequestHeader(..)。此方法使用 2 个参数,标头名称和标头值。所有这些操作都是在

xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

setRequestHeader(..) 方法必须应用于请求之后行中实现的,请求的特征是 open(...) ) 方法,但之前最终请求使用 send(.) 方法发送。

open(...)方法定义: (1) HTTP请求的类型,例如GET/POST/PUT等; (2) 包含此请求的处理脚本的网页,例如对后端数据库进行适当查询的某些 .php 文件或 Node.js 请求端点; (3)请求动态的性质,例如,异步请求被分配值“真”,同步请求被分配值“假”。

send(.) 方法附加要在请求正文中发送的数据,在您的例子中是名为“参数”的变量。

关于使用 setRequestHeader(..) 的更广泛的问题,我想说它在大多数 HTTP 请求情况下使用。但是 某些类型的数据添加到了正文中HTTP 请求调用“Content-Type”标头的默认设置。

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.

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.

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

<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

const 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.

We have to tell the receiving computer how the data within the HTTP request body is encoded. There is a standard header to 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

xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

This setRequestHeader(..) method must be applied to the request after the request is characterized with the open(...) method but before the final request is sent off with the send(.) method.

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'.

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 data added to the body of a HTTP request invoke a default setting for the 'Content-Type' header.

贪了杯 2025-01-03 04:48:42

正是它所说的。它将为下一个 XMLHttpRequest 设置“标头”信息。

标头几乎是一个键/值对。它用于将“元”信息传输到正在进行的请求的目标服务器。在您的特定实例中,它用于告诉服务器此请求使用哪种内容类型。

It is exactly what it says. It will set a "header" information for the next 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.

寂寞笑我太脆弱 2025-01-03 04:48:42

它将 Content-type HTTP 标头设置为包含从表单发送的 url 编码数据。

It sets the Content-type HTTP header to contain url encoded data sent from a form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文