2011-05-06 37 views
2

我用jQuery做了一個AJAX函數。該功能的正常使用,以及服務器返回一個JSON,但我不能在我的JS腳本使用它AJAX調用後檢索JSON的問題

服務器端:

$array = array(); 

$array["count"] = count($accounts_list).""; 

    for($i=0; $i<count($accounts_list); $i++) 
    { 
     $account = $accounts_list[$i]; 

     $array["marker"]["name"] = $account->name; 
     $array["marker"]["lat"] = $account->map_latitude; 
     $array["marker"]["lon"] = $account->map_longitude; 
    } 

    echo json_encode($array); 

客戶端:

$.ajax({ 
    type: "POST", 
    url: "index.php?module=cap_Maps&action=AddMarkers", 
    data: dataString, 
    dataType: "json", 
    success: function(data) { 
     if (data == "error"){ 
      $(".tr_legend").before("<tr><td colspan='2' id='error'><span class='error_maps'>Erreur lors du chargement des marqueurs</span><td><tr>"); 
     } 
     else { 
      alert(data); 
       var obj = jQuery.parseJSON(data) 
       alert (obj.count); 
     } 

    } 
}); 

JSON由服務器返回:

{"count":"371","marker":{"name":"WAMPFLER","lat":"49.02751610","lon":"2.10611230"}} 

功能alert(data)返回我JSON,但如果我嘗試用解析它,它不起作用,並且alert(obj.count);未打開。

編輯

我有加誤差函數:

error: function (XMLHttpRequest, textStatus, errorThrown) { 
        alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown); 
       }, 

而且出錯:

XMLHttpRequest={"count":"358"} 
textStatus=parsererror 
errorThrown=Invalid JSON: {"count":"358"} 

編輯

如果我有contentType: "application/json",我的AJAX,我可以回到這被認爲是JSON的靜態字符串,但如果我嘗試在我的服務器上執行其他PHP代碼,AJAX返回500 Internal server error

+0

在[解決方案]之前詢問類似的問題(http://stackoverflow.com/questions/631418/jquery-getjson-ajax-parseerror)檢查了這一點。希望這有助於 – 2011-05-06 09:14:28

+0

我真的不明白爲什麼jQuery會拋出這個錯誤; '{「count」:「358」}'是完全有效的。 – 2011-05-06 14:32:27

+0

我可以發送一個關聯數組而不是JSON嗎?如果我這樣做,我不會有與JSON的問題;) – Elorfin 2011-05-09 08:18:26

回答

0

輸出緩衝區沒有空,所以AJAX接收緩衝區內容+ JSON,所以它不是一個有效的JSON。

我在輸出我的JSON之前在服務器端使用ob_clean()並解決了問題。

0

jQuery.ajax

json類型將獲取的數據文件解析爲JavaScript對象,並將構造的對象作爲結果數據返回。

所以你不需要jQuery.parseJSON,因爲返回的字符串已經被解析。

+0

我只是發現'dataType:「json」'出錯:我無法執行函數'success'。當我嘗試'alert(data.count)'時,它是'undefined',所以我認爲它不是解析。 – Elorfin 2011-05-06 09:42:54

+0

@Corum:然後添加一個'error'回調函數,看看錯誤是什麼。 – 2011-05-06 12:01:08

+0

如果我指定'dataType:「json」',那麼後面的代碼不會被執行。如果我刪除這行,並用Firebug偵聽'jQuery.parseJSON(data)',我有錯誤'無效的JSON:[{「count」:「358」}]' – Elorfin 2011-05-06 13:41:56

1

試試這個:

var json = jQuery.parseJSON('{"count":"371","marker":{"name":"WAMPFLER","lat":"49.02751610","lon":"2.10611230"}}'); 

alert(json.count); 

也許你忘了給一個字符串 '' 到parseJSON。

+0

是的,我忘記了數據引用,但是如果我嘗試添加這個。我有一個錯誤在螢火蟲'JSON.parse [stopper sur une erreur](function(a,b){function cw(a){return f ... a:a +「px」)}}),a.jQuery = a。$ = f})(window);' – Elorfin 2011-05-06 09:52:56

0

而不是使用parseJson你可以嘗試使用eval。

$.ajax({ 
    type: "POST", 
    url: "index.php?module=cap_Maps&action=AddMarkers", 
    data: dataString, 
    dataType: "text", 
    success: function(data) { 
     if (data == "error"){ 
      $(".tr_legend").before("<tr><td colspan='2' id='error'><span class='error_maps'>Erreur lors du chargement des marqueurs</span><td><tr>"); 
     } 
     else { 
      alert(data); 
       var obj = eval(data); 
       alert (obj.count); 
     } 

    } 
}); 

HTH。

0

它看起來像你沒有發送適當格式的JSON從你的服務器嘗試使用json_encode()函數。

+0

閱讀代碼,我使用它;) – Elorfin 2011-05-10 07:22:33