2016-06-28 16 views
1

我想弄清楚如何從我的PHP腳本返回錯誤的我的jQuery功能。我已經用this question的最佳答案去了,在那裏我回顯了一個json對象,我在數據對象中返回,但是我看不到json對象的各個部分。如何從帖子的返回中訪問數據json對象?我的控制檯始終顯示沒有數據錯誤。數據如何在jQuery文章中返回?

$.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 
+2

看起來好像你的xdebug ON會在你的輸出中添加額外的內容(以及'error !!!') – Neal

+0

請確保在你的生產代碼中,不可能有人通過你的SQL查看你的SQL或表格細節一個錯誤。 – 4castle

+0

一旦你修復了php的錯誤/警告,你會想要刪除'echo「錯誤!!!」;'並且只是回顯json。這將是畸形的,如果你不 – Rasclatt

回答

2

你的PHP腳本輸出PHP錯誤,這導致JSON是無效的。

您可以從顯示禁用錯誤:

error_reporting(0); 

或修復錯誤。

+0

我已將'error_reporting(0);'添加到我的php代碼中,並且我還刪除了該錯誤!回聲。我仍然'沒有數據錯誤'是否還有其他東西我失蹤? – john

+0

如果SQL是成功的,那麼響應不是JSON。也許看看那個。 –

+0

嗯,這是JSON,把它需要被解析'var response = jQuery.parseJSON(data);'這個技巧並使它工作。感謝您的建議。 – john