[英]jQuery AJAX 200 status, yet mysterious syntax error
當我使用jQuery AJAX時,我返回了狀態200。 但是,我也從某個地方收到語法錯誤。 我發布到這樣的PHP:
function submit_order(orderInformation) {
$.ajax({
type: 'post',
url: 'queries/submit_order.php?<?=time();?>',
data: 'orderInformation=' + JSON.stringify(orderInformation),
dataType: 'json',
success: function (returnedData) {
console.log(returnedData);
$('#content_container').fadeOut(340, function () {
var new_content = $('#content_container').clone(false);
$('#content_container').remove();
new_content.css('display', 'none');
new_content.children().remove();
new_content.appendTo('body');
$('#content_container').vkTemplate('templates/confirm_template.tmpl?<?=time()?>', returnedData, function (el, data, context) {
console.log('success');
$('#content_container').fadeIn(340);
});
});
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
}
我的PHP代碼非常簡單:
$order_information = json_decode($json_str, true);
//go through the array and make an email out of it
//add a few elements to the array
//send the email
//send back a json string with the added elements
echo json_encode($order_information);
但我得到了這個:
奇怪的是,如果我將來自console.log(JSON.stringify(orderInformation))
的JSON字符串復制粘貼到PHP頁面中:
$json_str = '{"sector_0":{"file":[],"sector_info":{"sector_label":"NIO","purchase_order":"test","proof":false},"lines":{"line_0":{"description":"test","quantity":"2","productId":"1","addressId":"20","shipViaId":"1","notes":false}}}} ';
一切正常。 這個錯誤是什么? 這個<
在錯誤中看到的來自哪里?
謝謝
它是你的錯誤處理程序被觸發和日志:
請注意,帶有dataType: json
$.ajax
將觸發錯誤處理程序,即使服務器返回200 OK
但響應是無效的JSON。 語法錯誤不在JavaScript代碼中,而是在JSON中。 確定<
來自哪里,並確保您的PHP腳本發送有效的JSON。
提示:打開控制台並查看網絡選項卡; 所有XHR都與標題和正文一起記錄在那里。
200 - 服務器的回復是好的http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
您的響應服務器中存在語法錯誤,返回無效的json
由於您的PHP代碼接縫很好,所以必須有其他東西。 語法錯誤或你的框架返回json包裝的json ...
使用適當的工具查看服務器返回的內容。 (關於chrome上firefox /開發人員工具的firebug)
在你的圖像中你看到0: "<"
這意味着返回的字符串以<
- 開頭,這意味着返回的是html。
看起來你使用chrome。 轉到chrome中的“網絡”標簽,您應該可以看到針對您的請求的原始響應。
所以這是一個PHP錯誤:
$sector_index
是不可以忍受的。 你可以var_dump看看。 這是什么?
看起來<?=time()?>
沒有得到處理。 在發布到用於驗證的URL之前提醒URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.