注:请求地址是本身的项目地址,请自行更改。ajax
这只是一个简单的原生XMLHttpRequst的使用,以后会发如何封装原生ajax实现jequery的ajax
const xhr = new XMLHttpRequest();
xhr.open('PUT','http://118.24.84.199:8080/sm/accept/list',false);
xhr.setRequestHeader('token','515b8c62-ddf4-41ef-a7c8-93957e1c589e'); xhr.setRequestHeader('Accept','application/json'); xhr.setRequestHeader('Content-Type','application/json');
注意:这里的数据须要进行处理,处理为json文件,使用JSON.stringify处理。
let data = { page:1, pageSize:10, }; data = JSON.stringify(data); xhr.send(data);
若是数据是同步请求:直接在send()语句以后对数据进行处理。
console.log(xhr.response);
可是通常状况下数据的请求都是异步的,那么就要使用onreadystatechange这个事件对数据进行处理。
接收到数据以后将其打印。
xhr.onreadystatechange = function(event){ if (xhr.readyState == 4){ if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ console.log(JSON.parse(xhr.response)); } else { console.log("Request was unsuccessful: " + xhr.status); } } };