2017-04-08 85 views
-1

我的头:简单的jQuery的.load()不工作

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

我的身体:

<button id="load">Test</button> 
<div id="results"></div> 

我的jQuery负载(</body>前):

$('#load').click(function() { 

    $('#results').load("testLoading.php", null, function(responseText, textStatus, xhr) { 
     if (textStatus == "error") { 
      var msg = "Sorry but there was an error: "; 
      alert(msg + xhr.status + " " + xhr.statusText); 
     } 
    }); 
}); 

testLoading.php页面与代码在文件的同一个目录中,它只打印“hel LO”。所以没有不可能的,对吧?但是当我点击我的测试按钮时,没有任何反应。

该提示显示“对不起,但出现错误:0错误”。

我在DW代码:

enter image description here

控制台:

enter image description here

+0

当进行网络开发时,当某些事情不起作用时,首先要做的事情是打开Web控制台(通过菜单或大多数浏览器上的Ctrl + Shift + I或F12),这是可能告诉你,ajax呼叫被禁止。接下来的事情是在开发工具中使用调试器。 –

+0

不,我在Safari上。令人难以置信的事情可能是一年前,我可以通过另一个项目 – Skie

+0

这样做,控制台说:XMLHttpRequest无法加载http:// localhost:8888/testLoading.php。 Access-Control-Allow-Origin不允许Origin null。 – Skie

回答

0

显然,任何工具你使用(?DW)正在运行的代码,这样底层浏览器它使用的引擎认为请求来自file: URL(源自于null),或者您安装了一些Safari扩展程序导致那种事情的方式。

代码没有问题(你不需要那个null,但它是无害的)。在正常的环境中,该代码绝对有效。