jquery加载简单数据

时间:2014-03-25 07:01:38

标签: javascript jquery html

我正在使用jquery加载存储在' result.html'中的html内容。程序中的路径是正确的。但是,当我点击按钮时,内容没有被加载。这是" main.html"文件:

<html>
<head>
<title>the title</title>
<script type="text/javascript" src="jquery/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
   $("#driver").click(function(event){
      $('#stage').load("jquery/result.html");
   });
});
</script>
</head>
<body>
<p>Click on the button to load result.html file:</p>
<div id="stage">STAGE</div>
<input type="button" id="driver" value="Load Data" />
</body>
</html>

这是&#39; result.html&#39;文件:

 <html>
    <body>
    <h1>Result</h1>
    </body>
    </html>

2 个答案:

答案 0 :(得分:2)

在您的控制台中,您应该收到以下错误

enter image description here

  

否&#39;访问控制 - 允许 - 来源&#39;标头出现在请求的资源上。

您正在尝试向其他域执行XMLHttpRequest(AJAX)

由于安全原因,这是受限制的。

了解更多详情,请参阅: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

答案 1 :(得分:0)

您的代码基本上适合我。您的网址必定存在问题。如果我复制你的代码并在本地尝试使用另一个文件,它可以正常工作。

如果您从静态文件运行代码,请尝试将其切换到任何网络服务器。它可以产生很大的差异,因为如果您从静态文件中使用它们,某些浏览器不允许向本地URL发送Ajax请求。