js 请求接口ajax

 // 创建 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();
    ```

发布了43 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/darkCloudss/article/details/105528166
今日推荐