// 创建 XMLxhrRequest 实例
var xhr = new XMLHttpRequest();
// 发送请求的方式 和 URL 地址
xhr.withCredentials = true;//设置携带cookie跨域
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //添加请求头
xhr.open('POST',url,false);
xhr.onreadystatechange = function(){
if (xhr.status === 200 && xhr.readyState === 4) {
console.log(xhr.responseText);
var res = xhr.responseText;// 响应体数据
console.log(JSON.parse(res));// 将 json 数据转化为js数据
}
};
// 发送请求
xhr.send();
```
js 请求接口ajax
最新推荐文章于 2024-02-15 00:15:00 发布