我正在尝试发送带有标头的XMLHttpRequest并添加FormData。有没有一种(优雅的)方式我可以做这样的事情:
var formData = new FormData(); formData.append("file", file); var xhr = new XMLHttpRequest(); xhr.open("POST", "/ajax_gateway.php?mod=fileupload", true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded") xhr.send(formData, "token=add");
您无法Content-Type在发送时指定标题,FormData因为该标题会"multipart/form- data"被浏览器自动设置为。您也可以设置其他标题,请尝试以下操作:
Content-Type
FormData
"multipart/form- data"
var formData = new FormData(); formData.append("file", file); formData.append("mod", "fileupload"); formData.append("token", "add"); var xhr = new XMLHttpRequest(); xhr.open("POST", "/ajax_gateway.php"); xhr.setRequestHeader("X-Answer", "42"); xhr.send(formData);