Files
ClassContent/历届学生/苏琪/2019-06-18/ajax.html
2024-09-27 02:06:13 +08:00

58 lines
1.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
// function getData (res) {
// console.log(res)
// }
var http = new XMLHttpRequest()
http.open("GET","http://127.0.0.1/index.php")
http.send()
http.onreadystatechange = function () {
if(http.readyState == 4){
console.log(JSON.parse(http.responseText))
}
}
// 跨域 Access-Control-Allow-Origin
// 谁导致的? 浏览器
// 原因:浏览器有个同源策略
// 作用保护后端的api数据安全
// 当前页面的访问地址和你ajax需要访问的地址是否完全一致
// 1: 端口是否一致
// 2: 协议是否完全一致
// 3: 域名是否一致
// a https://www.a.com b http://www.a.com
// 跨域解决方案
// 1: 后端设置cros允许前端跨域
// 2: jsonp
// 它不是ajax请求只是一个普通的js文件资源请求浏览器不会拦截文件请求然后用过 callback 方式回调数据
// 3: 后端代理(nginx)
// A 前端 --> C 后端(别人)
// A 前端 ---> B 后端(自己) cors
// B 后端 ---> C 后端(别人)
// C ---> B ---> A
</script>
<!-- <script src="http://127.0.0.1/index.php?cb=getData"></script> -->
</body>
</html>