且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用纯JavaScript设置AJAX内容类型(无JQuery)

更新时间:2023-11-09 21:50:16

要设置内容类型,您需要修改

In order to set the content type you need to modify the request header.

我不知道您要设置哪种内容类型,所以我默认使用json。

I don't know which content type you'd like to set, so I've defaulted to json.

以下是设置方法:

var fd = new FormData();
fd.append("upload-file", document.getElementById('upload-file').files[0]);

var xhr = new XMLHttpRequest();

/* I add some event listeners here for progress, load, error, and abort */

xhr.open("POST", "upload.php");
xhr.setRequestHeader("Content-type","application/json");
xhr.send(fd);

编辑: 从MDN设置HTTP请求标头