这对其他人来说显然是显而易见的,但对于我的生活,我无法弄清楚。为什么w3schools的这个JQuery.get例子不适合我?
我已经复制从http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_get
的HTML我改变了一行略微使不用彷徨URL指向全w3schools.com URL(这只是正常的例子页)。这是代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("http://www.w3schools.com/jquery/demo_test.asp",function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
此代码工作得很好,我W3Schools的网站上,但是当我把它粘贴到一个名为“hi.html”我家电脑上的新文件,并在Chrome中打开它,并Firefox,它失败了。
我直接复制并粘贴这个html,为什么会失败?
必须是CORS(跨源资源共享)问题。查看此链接以获得更好的理解:http://www.html5rocks.com/en/tutorials/cors/ – Satya 2014-09-06 04:59:34