简体   繁体   English

为ajax调用设置xmlhttpRequest.setRequestHeader?

[英]Setting xmlhttpRequest.setRequestHeader for an ajax call?

When I have an ajax call why should i set xmlhttpRequest.setRequestHeader ? 当我有一个ajax调用时,我为什么要设置xmlhttpRequest.setRequestHeader? is it not obvious from the ajax object that we are sending an ajax request. 从ajax对象中我们发送ajax请求是不是很明显。

You have to understand first that AJAX request are HTTP request. 您必须首先了解AJAX请求是HTTP请求。 When you add 当你添加

xmlhttp.setRequestHeader('Content-Type','XXXXXXXXX');

it adds an header that define what kind of data will be contained in the body of the request. 它添加了一个标头,用于定义请求正文中包含哪种数据。 application/x-www-form-urlencoded means that the data will be form data. application/x-www-form-urlencoded表示数据将是表单数据。 The data format could be something else. 数据格式可能是其他内容。 It's just that form data is the most common thing you will send to a server. 只是表单数据是您将发送到服务器的最常见的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM