打开浏览器,右键,选择检查,选择console
输入测试代码,然后回车,正常返回即可跨域请求
get请求
var xhr = new XMLHttpRequest();
xhr.open('GET', 'url');
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.log(xhr.responseText);
}
post请求:
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', 'url', true);
httpRequest.setRequestHeader("Content-type","application/json");
// 请求体
var obj = {
"input": "I lvve buy some cakes"
};
httpRequest.send(JSON.stringify(obj));
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText;
console.log(json);
}
};
如果如下类似显示,则不可跨域请求
标签:httpRequest,判断,console,跨域,接口,xhr,json,var From: https://www.cnblogs.com/wangyb56/p/16996355.html