我在代码中做错了什么?我正在进行一个jquery/json调用,以调用数据库中的数据


What am I doing wrong in the code?I am making a jquery/json call to call the data from the database

我已经粘贴了index.php和jsonPhp.php的代码。我是JSON的新手,并使用ajax学习JSON。在这里,我尝试使用json从服务器获取数据。当我点击SERVER DATA链接时,来自服务器的数据必须在不使用jQuery/json重新加载页面的情况下出现。我已经写了代码,但我没有让它工作。请帮忙。谢谢

<head>
    <title>JSON WITH PHP</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"
    type="text/javascript"></script>
    <script type="text/javascript">
        < ![CDATA[
        $(function () {
            $('#click').click(function () {
                $.post('jsonPhp.php', function (data) {
                    //$("#content").html(data)+'<br>';
                    var pushedData = jQuery.parseJSON(data);
                    var htmlData = "";
                    //loop through using jQuery
                    $.each(pushedData, function (i, field) {
                        htmlData = htmlData + '-' + field.id + ',' + field.place + ',' + field.description + '<br>';
                    });
                    $('#content').html(htmlData);
                });
            });
        });]] >
    </script>
</head>
<body>Click on the link below to get the data from the Server Dynamicallly!
    <br
    />
    <a href="#" id="click">Server Data</a>
    <div id="content"></div>
</body>

<?php
    $db = mysql_connect("localhost","root","")or die(mysql_error());
    mysql_select_db("places",$db) or die(mysql_error());
    if(isset($_POST['place']))
        $place=$_POST['place'];
    if(isset($_POST['description']))
        $description=$_POST['description'];
     $myrows = array();
     $result = mysql_query("SELECT * FROM search");
     while( $row = mysql_fetch_assoc($result) ) {
         $myrows[] = $row;
     }
    echo json_encode($myrows);
?>

尝试指定参数,通过POST请求发送,结果如下:

$.post('jsonPhp.php', { place:'myplace', /* other params */ }, function (data) { ...

您的jQuery帖子没有发布必要的项目。

使用此代码时要小心。