xhr.open("GET", "data/a.text", true)
xhr.send()
xhr.open("POST","/try/ajax/demo_post2.php",true);
xhr.setRequestHeader("content-type","application/x-www-urlencoded");
xhr.send("fname=Henry&lname=Ford")
XMLHttpRequest
对象的responeseText
和responseXML
属性
属性 | 描述 |
---|---|
onreadyStateChange | 存储函数,每当readyState属性改变,调用该函数 |
readyState | 存有 XMLHttpRequest 的状态 0:请求未初始化——未调用open() 1:请求连接已经建立——未调用send() 2:请求已发送 3:请求处理中 4:请求已完成,响应准备就绪 |
status | 200: "OK" 404: "Not Found" |
//1.创建Ajax实例
let xhr = new XMLHttpRequest(); //IE下为ActiveObject对象
//2.设置请求配置
xhr.open("GET", "data/a.text", true)
//3.事件监听:通过监听readyStateChange事件,获知AJAX状态改变
xhr.onreadyStateChange = function() {
//请求完成 获取服务器返回的响应头响应主体
if(xhr.readyState == 4 && xhr.status == 200 ) {
console.log(xhr.responseText)
}
}
//4.发送Ajax请求
xhr.send()
let xhr = new XMLHttpRequest()
xhr.open("GET", url, true)
xhr.onreadyStateChange = callback
xhr.send()
$.ajax(url, [settings])
$.ajax({
type: "GET", //请求类型
url:"test.html", //发送请求的地址
data: "", //发送到服务器的数据。将自动转换为请求字符串格式
dataType: "html", //预期服务器返回的数据类型
async: true, //请求是否异步,默认为异步
headers: {}, //设置消息头中的值
timeout: "", //设置请求超时时间(毫秒)
beforeSend: function(XMLHttpRequest) {
//请求前的处理——可修改XMLHttpRequest对象,如自定义请求头
},
success: function(data, status) {
//请求成功时处理
},
complete: function(XMLHttpRequest, status) {
//请求完成的处理
},
error: function() {
//请求出错处理
}
})
原文:https://www.cnblogs.com/zengbin13/p/12905303.html