简单jqueryajaxphp示例

简单jqueryajaxphp示例,php,jquery,ajax,Php,Jquery,Ajax,我一直在看一些介绍如何将ajax与jQuery结合使用的示例,但当我在服务器上尝试这些示例时,它们似乎不起作用 以下是html文件中的代码: <html> <head> <script src="jquery.js"></script> <script type="text/javascript"> $(function(){ $.get("ajax/json-statistics.php", function(data

我一直在看一些介绍如何将ajax与jQuery结合使用的示例,但当我在服务器上尝试这些示例时,它们似乎不起作用

以下是html文件中的代码:

<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
    $(function(){
    $.get("ajax/json-statistics.php", function(data){
        alert("hello");});
});

</script>
</head>
<body>
</body>
</html>


$(函数(){
$.get(“ajax/json statistics.php”,函数(数据){
警报(“你好”);});
});
以下是PHP文件中的代码:

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>


两个文件位于同一目录中。当我转到html页面时,它只是空白的,但我希望出现一个包含文本hello的警告框。

我发现这在我的工作中起作用

<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script LANGUAGE="JavaScript">
$(document).ready(function(){
$.get("ajax/json-statistics.php", function(data){
    alert("hello");}, "json");
});
</script>

$(文档).ready(函数(){
$.get(“ajax/json statistics.php”,函数(数据){
警报(“hello”);},“json”);
});

检查路径-它们是相对的,因此它们指向浏览器的文件应该在正确的位置(可能已经在-我们没有足够的数据)

如果它仍然不起作用,可能还有其他问题。也许你把JavaScript关掉了

检查是否已发送请求以及是否存在任何其他问题的最佳方法是使用一些调试工具。我建议使用Chrome的开发工具或Firefox的Firebug-与他们一起检查文件和数据请求是否发送到正确的位置,并检查是否存在任何其他JavaScript错误。

尝试:

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>


你也把
jquery.js
放在那里了吗?当两个文件都在同一个目录中时,那么为什么你会这样提到
$.get(“ajax/json statistics.php”),…
呢?应该是
$.get(“json statistics.php”),…
Ya抱歉,我已经修复了,但它仍然不起作用。当你要求任何东西时,你应该给出一个全面的图像,说明它到底是如何不起作用的。Firebug中是否有任何错误消息?请查看Firebug中的“网络”选项卡,单击XHR,然后告诉我们错误/响应是什么。希望你的php apache服务器正在运行:)它在我这方面仍然不起作用。我正在运行安装了PHP5.3.3的ubuntu服务器btw。请检查请求的页面路径是否正确。还要将返回类型指定为“json”。我无法更改您的json-statistics.PHP代码,并且在这里可以正常工作。