doublestar2014 2015-06-30 10:14
浏览 34
已采纳

jQuery没有推送JSON数据

I have the following script which is supposed to pull data and display it in a HTML table:

$('#search_filter_button').click(function (e) {
    e.preventDefault(); // Stop form submission
    var county = $('#filter_county').val(),
        kp_type = $('#filter_kp_type').val(),
        getUrl = window.location,
        baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];

    html_tr = '';
    $.ajax({
        type: 'GET',
        url: baseUrl + "/reports/get_report.php?county=" + county + "&kp_type=" + kp_type,
        dataType: "JSON",
        success: function (data) {
            console.log(data);
            for (i = 0; i < data.length; i++) {    
                html_tr += '<tr>
\
                <td>' + data[i].name + '</td>
\
                <td>' + data[i].Abbrv + '</td>
\
\
                <td>' + data[i].partner_name + '</td>
\
\
                <td>' + data[i].facility_name + '</td>
\
\
                <td>' + data[i].county + '</td>
\
\
                <td>' + data[i].no_kps + '</td>
\
\
                <td>' + data[i].activity_stamp + '</td>
\</tr>';
            }

            $('#tbody_append').empty();
            $('#tbody_append').append(html_tr);
        }, error: function (data) {
        }
    });
});

My get_report.php file looks like this:

include '../database/db_connect.php';

$mysqli = mysqli_connect($host_name, $user_name, $password, $database);

$county = $_GET['county'];
$kp_type = $_GET['kp_type'];

// get the records from the database
$result = mysqli_query($mysqli, "SELECT * FROM `no_individaul_kps_contacted` where county='$county'");

$count_row = mysqli_num_rows($result);
if ($count_row >= 1) {
    // display records if there are records to display

    while ($user_data = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        echo json_encode($user_data);
    }
} else {
    // show an error if there is an issue with the database query
    echo "Error: " . $mysqli->error;
}

// close database connection
mysqli_close($mysqli);

This pulls the information and echoes it back in JSON ENCODE format. But my JavaScript is not returning a success.
Please advise what am I doing wrong.

  • 写回答

1条回答 默认 最新

  • douju7765 2015-06-30 10:21
    关注

    Collect the user data into array inside the while loop and move json_encode($data) outside the while loop:

    $select = "SELECT * FROM `no_individaul_kps_contacted` where county='$county'";
    $result = mysqli_query($mysqli, $select);
    $data = [];
    
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $data[] = $row;
    }
    
    echo json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。