2-10跨域失败
1.hosts文件
2.文件夹目录
3.html代码
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button id="btn">获取数据</button>
<pre id="store"></pre>
</body>
</html>
<script>
var btn = document.getElementById('btn');
var store = document.getElementById('store');
btn.addEventListener('click', ajax);
function ajax() {
var xhr = new XMLHttpRequest();
var src = 'http://sub.local.com/cors-data.php';
xhr.open('POST', src);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
store.innerHTML = xhr.response;
}
};
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('name=abc');
}
</script>
4.php代码
<?php
//sub.local.com
sleep(1);
header('Access-Control-Allow-Origin:*');
echo "{\"data\":{},\"errno\":0}"
?>
5.wamp中测试
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 467 份
- 解答问题 4826 个
本路径带你通过系统学习HTML5、JavaScript、jQuery的进阶知识,不仅如此,还会学习如何利用组件化的思想来开发网页,知识点+案例,使得所学可以更好的得到实践。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星