简体   繁体   English

jQuery帖子中的数据如何返回?

[英]How is data returned in a jquery post?

I'm trying to figure out how to return error's from my php script to my jquery function. 我试图弄清楚如何从我的PHP脚本向我的jquery函数返回错误。 I've gone with the top answer from this question where I echo a json object that I return inside the data object but I cannot see the individual parts of the json object. 我已经回答了这个问题的最高答案,即我在数据对象内部回显了一个json对象,但是看不到json对象的各个部分。 How do I access the data json objects from the return of the post? 如何从帖子的返回处访问数据json对象? my console always shows no data error. 我的控制台始终不显示任何数据错误。

$.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 script 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;

}

my console shows this 我的控制台显示了

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

Your PHP script is outputted PHP errors, this causing the JSON to be invalid. 您的PHP脚本输出了PHP错误,这导致JSON无效。

You can either disable errors from showing: 您可以禁用以下错误:

error_reporting(0);

or fix the errors. 或修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM