快速测试某个请求在浏览器中是否跨域
在浏览器中打开F12 打开控制台
键入以下代码 回车即可
/*
* @Description: 测试在浏览器中是否跨域
* @Author: 仲灏<izhaong 164165005@qq.com>
* @Date: 2020-12-16 10:02:08
* @LastEditors: 仲灏<izhaong 164165005@qq.com>
* @LastEditTime: 2020-12-17 10:09:56
*/
(function loadXMLDoc() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatecatechange = function () {};
xmlhttp.open(
"get",
"https://zjksmartbus.toponedt.com/travelpc/vendor/list",
true
);
xmlhttp.setRequestHeader(
"token",
"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI2IiwiY3JlYXRlZCI6MTYwODE3MDgzNzQ5MywiZXhwIjoxNjA4MTc4MDM3fQ.jwUzGz8OCJz2hfQlBQkB50j1ZYpJnfMn4OdeBPO5upIwdbpjrtFt4iOi38L_zu9l_jEmEVHHk0G9jIOQMoHcDQ"
);
xmlhttp.withCredentials = true;
xmlhttp.send();
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
以上出现 cors 即为跨域
# post请求
例如 登录接口
(function loadXMLDoc() {
var request = new XMLHttpRequest();
request.open('POST', 'http://××××/api/login');
request.setRequestHeader("Content-type","application/json");
send_data = {
"mobile": "11",
"password": "1",
"smsCode": "1111",
"type": 0
}
request.send(JSON.stringify(send_data));
request.onload = function(e){
console.log("请求成功")
console.log(request.status, "请求的URL的相应状态")
console.log(request.readyState, "请求的结果,一般都是4")
if (request.status === 200) {
alert('设置回调事件成功!');
}
else{
alert('设置失败,请重试!');
}
}
request.onerror = function(e){
alert('请求失败')
}
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
上次更新: 2022/06/05, 20:31:36