derek5. 2018-01-04 08:38 采纳率: 100%
浏览 48

Ajax的标头是做什么的

What is Ajax's header's role? Why do we need this?

And in this specific case below, what cause the differences between with xhttp.setRequestHeader() and without?

<!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>
  • 写回答

1条回答 默认 最新

  • weixin_33744141 2018-01-04 08:44
    关注

    HTTP headers are used to send additional data, or metadata, along with the main body of the request to provide information, such as the content-type, to either the server or the browser; for example, the browser may tell the server that it expects the server to respond with JSON encoded data.

    In this case, setting the content-type to x-www-form-urlencoded tells the server that form data encoded as a URL query-string (e.g. using ?name=value&othername=othervalue). If you wanted to upload a file instead of sending regular form data, you would use multipart/form-data as the content-type instead

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退