admin 管理员组文章数量: 1086019
I'm attempting to send one file and one text variable to my server with the FormData object. Looking at the Network tab in Chrome's developer tools, I can see that the file and variable are being sent. However, I've tried var_dump() on the $_POST and $_FILES variables, and both are shown as empty arrays. Here's the code I'm using for the form:
var image_upload = document.getElementById("image_upload");
if(image_upload.value == '')
{
alert("Please select a file to upload.");
}
else
{
alert("in");
var ajaxHandler = new XMLHttpRequest();
var content = image_upload.files[0];
var formData = new FormData();
formData.append("type", "5");
formData.append("content", content)
ajaxHandler.onreadystatechange = function() {
if(ajaxHandler.readyState == 4)
{
alert(ajaxHandler.responseText);
}
};
ajaxHandler.open("POST", "newCard", false);
ajaxHandler.setRequestHeader("Content-type","multipart/form-data");
ajaxHandler.send(formData);
}
I have tried this code with and without "charset=utf-8" in the content type, and it doesn't seem to make a difference. What's going on here?
I'm attempting to send one file and one text variable to my server with the FormData object. Looking at the Network tab in Chrome's developer tools, I can see that the file and variable are being sent. However, I've tried var_dump() on the $_POST and $_FILES variables, and both are shown as empty arrays. Here's the code I'm using for the form:
var image_upload = document.getElementById("image_upload");
if(image_upload.value == '')
{
alert("Please select a file to upload.");
}
else
{
alert("in");
var ajaxHandler = new XMLHttpRequest();
var content = image_upload.files[0];
var formData = new FormData();
formData.append("type", "5");
formData.append("content", content)
ajaxHandler.onreadystatechange = function() {
if(ajaxHandler.readyState == 4)
{
alert(ajaxHandler.responseText);
}
};
ajaxHandler.open("POST", "newCard", false);
ajaxHandler.setRequestHeader("Content-type","multipart/form-data");
ajaxHandler.send(formData);
}
I have tried this code with and without "charset=utf-8" in the content type, and it doesn't seem to make a difference. What's going on here?
Share Improve this question asked Apr 26, 2012 at 3:47 FibericonFibericon 5,80313 gold badges39 silver badges65 bronze badges1 Answer
Reset to default 8Remove the ajaxHandler.setRequestHeader("Content-type","multipart/form-data");
from the code.
The proper multipart/form-data header should contain boundary string. Browser automatically set that header, if you add file in FormData.
本文标签: phpUsing FormData object the server receives an empty POSTStack Overflow
版权声明:本文标题:php - Using FormData object, the server receives an empty POST - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744067554a2527883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论