我一整天都在寻找并且无法解决这个问题。
我刚开始在课堂上学习AJAX。对于一个小项目,我们试图从IIS中的Windows Server 2012 R2托管的网站上获取json信息。
但是,运行以下javascript命令的本地网页在尝试建立连接时不断出现此错误。
XMLHttpRequest无法加载' URL'。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。来源'网址'因此不允许访问。
这是代码:(我主演了我试图请求的域名)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ajax Test</title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"</script>
</head>
<body>
<button id="btn">Get File</button>
<div id="blank">
</div>
</body>
<script>
var button = document.getElementById("btn");
var myData;
button.addEventListener("click", function(){
$.ajax({
dataType: "html",
url: "http://**.***.**.*/test.html"
}).success(function(data){
myData = data;
console.log(myData);
})
})
</script>
</html>