如何在jquery帖子中返回数据?

时间:2016-06-28 18:25:15

标签: javascript php jquery

我正在试图弄清楚如何将错误从我的php脚本返回到我的jquery函数。我已经找到了来自this question的最佳答案,其中我回显了一个json对象,我在数据对象中返回但我看不到json对象的各个部分。如何从帖子的返回中访问数据json对象?我的控制台始终显示没有数据错误。

$.post("../dist/scripts/submitOrder.php",
    {
        clientID: clientID,
        date: date,
        po: po,
        orderType: orderType,
        orderlines:JSON.stringify(productArray)
    },
    function(data, status){
        console.log("Data: " + data + "\nStatus: " + status);
        if(data.error){
            console.log("BIG ERROR:" + data.error.message);
        } else {
            console.log("no data error");
        }
    })
        .fail(function() {
        console.log("Fail Data: " + data + "\nStatus: " + status);
    });

relavent .php脚本

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully ";
} else {
    $conn->close();
    //echo "Error: " . $sql . "<br>" . $conn->error;
    echo "error!!!";
    echo json_encode(array('error' => array(
    'message' =>  $sql,
    'code' => $conn->error,
    )));
    exit;

}

我的控制台显示了这个

Data: <br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: clientID in C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php on line <i>11</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>259296</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php' bgcolor='#eeeeec'>..\submitOrder.php<b>:</b>0</td></tr>
</table></font>
error!!!<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: main(): Couldn't fetch mysqli in C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php on line <i>32</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>259296</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\admin\startbootstrap-sb-admin-2-1.0.8\dist\scripts\submitOrder.php' bgcolor='#eeeeec'>..\submitOrder.php<b>:</b>0</td></tr>
</table></font>
{"error":{"message":"INSERT INTO Orders (clientID, dateSubmitted , type)\r\nVALUES ('0', '', 'internal')","code":null}}
Status: success
VM256:279 no data error

1 个答案:

答案 0 :(得分:2)

您的PHP脚本输出PHP错误,导致JSON无效。

您可以停止显示错误:

error_reporting(0);

或修正错误。