var xhr = new XMLHttpRequest ();
xhr.open('get|post' , url)
post --? xhe.setRequestHeadr('Content-Type', "application/x-www-form-urlencoded")
xhr.send()
xhr.readstatechange = function(){
xhr.onload = function(){
console.log(xhr.responseText)
}
}
上传文件的 时候 ---? post
var fd = new FormData( 表单的DOM对象 ) 自动拿到的是代码有name 字段
fd.append('avatar', this.files[0])
fd.set()
var xhr = new XMLHttpRequest ();
xhr.open('get|post' , url)
xhr.send(fd)
xhr.readstatechange = function(){
xhr.onload = function(){
console.log(xhr.responseText)
}
}
上传图片
$.ajax({
processData: false,
contentType: false
})
jsonp
$.ajax({
dataType:"jsonp"
})
jsonp 原理 script src 发送请求 ?callback=fn
前端 script src = "baidu.com?callback=fn"
后端 res.send ( req.query.callback + '( )' )