判断接口是否支持跨域

打开浏览器,右键,选择检查,选择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);
  }
};

如果如下类似显示,则不可跨域请求

 

posted @   wangyb56  阅读(1077)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示