使用AJAX将数据从PHP返回到JavaScript - 数据类型错误?

时间:2018-05-27 19:22:57

标签: javascript php mysql ajax

我正在使用AJAX将查询返回给我的JavaScript函数,但是我在返回json_encode时遇到了问题。我可能在某个地方的数据类型中有错误,但想在这里查看一些建议。

这是我的AJAX请求:

$.ajax({
            url: 'testajax.php',
            method: 'POST',
            data: {value},
            dataType:'json',
            success: function(data){
                console.log(data);}
});

这是我的PHP脚本:

<?php
require 'login.php';
$connection = new mysqli($host, $user, $pword, $database, 3306);
$insertquery = $connection->prepare("SELECT tracking_type, tracking_change_date, vessel_fcm_new, vessel_hull_id_new, vessel_name_new, vessel_length_new, vessel_manufacturer_new, vessel_manufacturer_id_new, vessel_year_new, vessel_value_new, owner_id_new, loss_payee_id_new, policy_id_new, policy_start_date_new, policy_end_date_new FROM tracking WHERE tracking_type = ?");
$insertquery->bind_param("i", $trackingtype);
//$trackingtype = $_POST['value'];
$trackingtype = 1;
$insertquery->execute();
$insertquery->bind_result($tracking_type, $tracking_change_date, $vessel_fcm_new, $vessel_hull_id_new, $vessel_name_new, $vessel_length_new, $vessel_manufacturer_new, $vessel_manufacturer_id_new, $vessel_year_new, $vessel_value_new, $owner_id_new, $loss_payee_id_new, $policy_id_new, $policy_start_date_new, $policy_end_date_new);
while ($insertquery->fetch()){
    $data = array($tracking_type, $tracking_change_date, $vessel_fcm_new, $vessel_hull_id_new, $vessel_name_new, $vessel_length_new, $vessel_manufacturer_new, $vessel_manufacturer_id_new, $vessel_year_new, $vessel_value_new, $owner_id_new, $loss_payee_id_new, $policy_id_new, $policy_start_date_new, $policy_end_date_new);
    echo json_encode($data);
}

如果我只有1行匹配此查询,那么它可以正常工作。但是一旦我添加了与select查询匹配的另一行,它就无法正确返回到JavaScript函数,但在浏览器中显示正常(如果你访问testajax.php)。

目前这是返回数据的方式。也许我在使用的数据类型中有错误:

["Insert","2018-05-26","JBL5693",null,"Makers Mark","22","sdgfsg3","256632asdasd",2014,263,"217","11",null,null,null]["Insert","2018-05-27","fFH465","FDDEE453","GIIGE","22","Shippers","2432465we",2014,205222,"Smith Jones","Capital One",null,null,null]

4 个答案:

答案 0 :(得分:1)

这个位可能会给你一个线索如果我只有1行匹配这个查询,那么它可以正常工作

因此,将数据保存在数组中,然后像这样发送整个数组

while ($insertquery->fetch()){
    $data[] = array($tracking_type, $tracking_change_date, $vessel_fcm_new, 
                $vessel_hull_id_new, $vessel_name_new, $vessel_length_new, 
                $vessel_manufacturer_new, $vessel_manufacturer_id_new, 
                $vessel_year_new, $vessel_value_new, $owner_id_new,     
                $loss_payee_id_new, $policy_id_new, $policy_start_date_new, 
                $policy_end_date_new);
}
echo json_encode($data);

编辑:

对此代码的简单测试将是

$data[] = array('Insert','2018-05-26', 1);
$data[] = array('Insert','2018-05-26', 2);

echo json_encode($data);

这将生成

[
 ["Insert","2018-05-26",1],
 ["Insert","2018-05-26",2]
]

而不是您建议的样本输出。

答案 1 :(得分:0)

您应该做的是返回一个对象数组。不是多个对象。

$to_return = [];
while ($insertquery->fetch()){
    $data = array($tracking_type, $tracking_change_date, $vessel_fcm_new, $vessel_hull_id_new, $vessel_name_new, $vessel_length_new, $vessel_manufacturer_new, $vessel_manufacturer_id_new, $vessel_year_new, $vessel_value_new, $owner_id_new, $loss_payee_id_new, $policy_id_new, $policy_start_date_new, $policy_end_date_new);
    $to_return[] = $data;
}

echo json_encode($to_return);

答案 2 :(得分:0)

问题是你为每次循环打印一个列数组,因此JSON没有被正确编码为数组数组;您可以按如下方式修改代码:

$results = array();
while ($insertquery->fetch()) {
  $newResult = array($tracking_type, $tracking_change_date, ...);
  array_push($results, $newResult);
}
$encoded = json_encode($results);
echo $encoded;

将打印出类似的内容:

[
    ["Insert","2018-05-26","JBL5693", ...],
    ["Insert","2018-05-27","JBL5694", ...],
    ...
]

请注意,最好将客户端&lt;&gt;服务器接口设计为使用对象数组,例如:

[
    {"tracking_type": "Insert", "tracking_change_date": "2018-05-26", "vessel_fcm_new": "JBL5693", ...},
    {"tracking_type": "Insert", "tracking_change_date": "2018-05-26", "vessel_fcm_new": "JBL5693", ...},
    ...
]

这种方法的优点是客户端客户端不需要知道列的顺序,而是可以直接访问它关心的属性。

答案 3 :(得分:0)

请务必保留

echo json_encode($data);

在while循环之外。