Javascript Ajax';s头怎么办

Javascript Ajax';s头怎么办,javascript,html,ajax,Javascript,Html,Ajax,Ajax的头的角色是什么?我们为什么需要这个 在下面这个特定的例子中,是什么导致了有xhttp.setRequestHeader()和无之间的差异 <!DOCTYPE html> <html> <body> <script> var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 &&

Ajax的头的角色是什么?我们为什么需要这个

在下面这个特定的例子中,是什么导致了有
xhttp.setRequestHeader()
和无
之间的差异

<!DOCTYPE html>
<html>
<body>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log(this.responseText);
    }
}
xhttp.open("POST", "demo_post2.asp", true);
// xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
</script>

</body>
</html>

var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
console.log(this.responseText);
}
}
xhttp.open(“POST”,“demo_post2.asp”,true);
//setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhttp.send(“fname=Henry&lname=Ford”);

HTTP头用于向服务器或浏览器发送附加数据或元数据,以及请求的主体,以提供信息,如内容类型;例如,浏览器可能会告诉服务器它希望服务器使用JSON编码的数据进行响应


在这种情况下,将
内容类型
设置为
x-www-form-urlencoded
会告诉服务器表单数据编码为URL查询字符串(例如,使用
?name=value&othername=othervalue
)。如果要上载文件而不是发送常规表单数据,您可以使用
多部分/表单数据
作为
内容类型

它将“数据类型”之类的信息发送到您的服务器,以便服务器能够正确地处理它。您可能会在这里对此类问题投反对票。请求标头会通知服务器生成的请求。在您的情况下,如果如果您将该行注释为
application/x-www-form-urlencoded
,则默认为。仅举一个例子,尝试阅读有关Corsb的内容,但实际上,如果没有它,日志将显示
Hello

,日志将显示
Hello Henry Ford

。这就是它真正让我困惑的地方,是不是有什么关于特定的asp导致了这一点?它在这里起了作用。我假设它与
“内容类型”
@David
应用程序/x-www-form-urlencoded相关
是默认值。不知道为什么不在本例中,此编码将
&
符号更改为
&处理时,这就是您看到的差异的原因。