这是我的代码:
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "LiveUpdate.php";
var sb = document.getElementById("LiveUpdate").value;
var FirstName = document.getElementById("FirstName").value;
var images = document.getElementById("images").value;
var vars = "update="+sb+"&FirstName="+FirstName+"&images="+images;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("success_insert").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("success_insert").innerHTML = "processing...";
}
我只想发送所有基本信息,如名字,中间名,姓氏和图像。问题是,我可以发送名字,中间名和姓氏,但我无法将图像传递给LiveUpdate.php端点。
发生了什么事?
答案 0 :(得分:0)
<input type="file">
元素存储用户在FileList
属性中选择的File
个.files
个对象,元素的.value
被设置为字符串。您可以将Content-Type
设置为multipart/form-data
,使用FormData()
,设置元素的name
属性来查询字符串键,.value
或.files
属性{ {1}}元素将设置为<form>
对象的值。
在FormData
event.preventDefault()
事件时致电submit
,将<form>
引用传递给form
构造函数,以设置FormData()
对象的键值对
FormData