2011-03-16 23 views
2

我的AJAX腳本都運行完美,並且成功功能正在工作,腳本仍然遇到錯誤消息。我確定問題是parseerror。我沒有確定的是爲什麼會發生這種情況,以及如何阻止它。JQuery AJAX莫名的解析錯誤

AJAX

var dataString = 'title=' + title + '&price=' + price + '&duration=' + duration + '&dives=' + dives + '&hire=' + hire + '&date=' + date + '&currency=' + currency + '&cost=' + cost + '&supplier=' + supplier; 

    $.ajax({ 
      type: 'POST', 
      url: '<?php echo $thisposturl?>?catadd', 
      data: dataString, 
      beforeSend: function() { 
       $('#loadwheel-new').html('<img id="BKloader" src="http://www.divethegap.com/update/z-images/structure/icons/ajax-loader.gif" alt="" width="30" height="30"/>'); 
       }, 
        error: function() { 
       $('#loadwheel-new').html('lkk'); 
       }, 
       dataType:'json', 
       success: function(data) { 
    $('#CollapsiblePanel' + data.CATid).load('<?php echo $thisposturl?> #' + data.CATid); 
    ; 
} }); 

PHP

$title = $_POST['title']; 
$CATid = $the_post_id; 
$date = get_the_time('Y-m-d'); 
$price = $_POST['price']; 
$duration = $_POST['duration']; 
$dives = $_POST['dives']; 
$hire = $_POST['hire']; 
$currency = $_POST['currency']; 
$cost = $_POST['cost']; 
$supplier = $_POST['supplier']; 


echo json_encode(array('title'=>$title, 'CATid'=>$CATid, 'date'=>$date, 'price'=>$price, 'duration'=>$duration, 'dives'=>$dives, 'hire'=>$hire, 'currency'=>$currency, 'cost'=>$cost, 'supplier'=>$supplier)); 
+0

並在服務器端或客戶端上出現解析錯誤? – 2011-03-16 14:21:53

+0

嚴謹的一面。這與JSON有關我認爲 – 2011-03-16 14:25:31

+1

您從服務器接收到的結果是什麼?你可以複製並粘貼使用螢火蟲。您應該能夠查看從服務器收到的響應。 – 2011-03-16 14:26:29

回答

0

調用$ .getJSON當我跑成這樣,但我認爲它歸結爲同樣的事情,畸形的JSON。我通過在JavaScript try/catch中包裝我的json解析來解決這個問題。

+0

您能詳細說明一下JacaScript try/catch嗎? – 2011-03-16 14:37:29

+0

當然 - 這不會解決你的JSON編碼錯誤,但至少會讓你知道出現了一些問題,提醒你查看它,並且如果JSON出現問題,也會停止整個頁面的失敗。我在你的Ajax成功回調函數中做了這樣的事情: try { var response = $ .parseJSON(response); 這裏//解析JSON }趕上(ERR){// 壞如果JSON,在發展中,我 可能只是CONSOLE.LOG(ERR) } – 2011-03-16 15:24:22

+0

嗯,不知道如何使代碼中的註釋顯示,對不起缺少格式化... – 2011-03-16 15:28:25

0

試過這個而不是?

var dataString = { 
    'title': title, 
    'price': price, 
    'duration': duration, 
    'dives': dives, 
    'hire': hire, 
    'date': date, 
    'currency': currency, 
    'cost': cost, 
    'supplier': supplier 
}; 
1

嘗試在服務器端的乾淨緩衝區ob_clean輸出的JSON之前