我正在尝试使用标头发送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");
答案 0 :(得分:3)
发送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);