65 lines
1.6 KiB
HTML
65 lines
1.6 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>ajax跨域</title>
|
||
</head>
|
||
<body>
|
||
|
||
|
||
|
||
|
||
|
||
</body>
|
||
|
||
<script>
|
||
|
||
// ajax 在调用接口的过程中会有一个访问的权限认证,这个过程叫 同源策略
|
||
// 同源策略 回去检查前端和后端的访问地址是否完全一致
|
||
// 1: 端口
|
||
// 2: 协议
|
||
// 3: 域名
|
||
|
||
// 报错
|
||
|
||
// Access to XMLHttpRequest at 'http://127.0.0.1/index.php?username=lb122&pwd=1234567890' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
|
||
|
||
// 解决跨域
|
||
|
||
// 1: cors
|
||
// Access-Control-Allow-Origin: *
|
||
|
||
// 2: jsonp
|
||
// jsonp不是ajax请求,他是js文件资源请求 + callback
|
||
|
||
// 3: 服务端代理
|
||
//
|
||
|
||
// 前端:http://127.0.0.1:5500/2020-04-08/dddF.html
|
||
// 后端:http://127.0.0.1:80/index.php
|
||
|
||
var http = new XMLHttpRequest();
|
||
http.open("GET", 'http://127.0.0.1/ziji.php')
|
||
http.send()
|
||
http.onreadystatechange = function () {
|
||
if (http.readyState == 4 ) { // 浏览器自带的
|
||
if (http.status == 200) { // 浏览器自带的
|
||
console.log(JSON.parse(http.responseText))
|
||
} else {
|
||
alert("网络不好")
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
|
||
// function getData(res) {
|
||
// console.log(res)
|
||
// }
|
||
|
||
|
||
|
||
</script>
|
||
<!-- <script src="http://127.0.0.1/index.php?username=lb122&pwd=1234567890&call=getData"></script> -->
|
||
</html> |