-
创建XMLHttpRequest异步对象
var xhr = new XMLHttpRequest() -
设置回调函数
xhr.onreadystatechange = callback -
使用open方法与服务器建立连接
// get 方式 xhr.open("get", "test.php", true) // post 方式发送数据 需要设置请求头 xhr.open("post", "test.php", true) xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") -
向服务器发送数据
// get 不需要传递参数 xhr.send(null) // post 需要传递参数 xhr.send("name=jay&age=18") -
在回调函数中针对不同的响应状态进行处理
function callback() { // 判断异步对象的状态 if(xhr.readyState == 4) { // 判断交互是否成功 if(xhr.status == 200) { // 获取服务器响应的数据 var res = xhr.responseText // 解析数据 res = JSON.parse(res) } } }
相关文章:
- Ajax发送请求的四个步骤 2021-11-05
- jquery ajax请求步骤 2021-11-04
- Ajax的get请求向服务器请求数据五步骤? 2021-12-03
- ajax请求的完整步骤 2021-11-05
- ajax----发送异步请求的步骤 2021-10-28
- Ajax实现异步请求步骤 2021-12-02