利用Ajax来提交图片
原生Ajax无法携带图片上传 formdata提交 Ajax2.0版
原生Ajax实现formdata
let form=documentElecmentById("form")
form.οnsubmit=function(){ //阻止默认事件return false
//创建 xml对象
let f = new FormData(form) //将整体form表单的所有数据 转化为formdata
let xml=new XMLHttpRequest()
//创建链接 .open (方法,地址,是否异步)
xml.open(form.method,form.action.true)
//发送请求 .send(数据)
xml.send(f)
//onreadystatechange 监听请求的状态变化
请求状态用 .readystate来表示
0 1 2 3 4 当状态等于4时候证明数据发送已经过来
//.status 请求状态 ==200时 表示请求成功
xml.onreadystatechange=function(){
//ajax 内容
if(xml.readystate==4&&xml.status==200){
//console.log(xml.responseText) //responseText后端返回过来的代码(前提后端返回的是字符串而不是对象 对象的话需要JSON.parse 解析后再输出)
let result =JSON.parse(xml.responseText)
console.log(result)
}
}
return false
}