2015-01-20 89 views
0

發送到某些 'request_url' HTTP請求返回格式{ '更迭':1 'HTML': 'thestuff'} JSON響應獲取JSON嵌套AJAX調用

所以當

jQuery.ajax({ 
    url: 'request_url', 
    success: function(response){ 
     alert(response.html); //'thestuff' is here as expected 
    } 
}); 

'thestuff'可以在預期的response.html中找到。但如果這個ajax在另一個ajax請求的'成功'回調中被調用,那麼response.html將變空,'thestuff'將變爲'響應'。

jQuery.ajax({ 
    url: 'some_other_url', 
    success: function(some_other_response){ 
     jQuery.ajax({ 
     url: 'request_url', 
     success: function(respose){ 
      alert(response.html); //there is nothing 
      alert(response);   //I see 'thestuff' which is expected in 'html' 
     } 
    }) 
    } 
}); 

爲什麼會發生?

更新:「thestuff」包含了一些JS代碼爲{}我想的東西可能會不知所措,但它爲什麼單(不嵌套)Ajax請求效果很好。

+0

無數錯別字VS'response' VS'respose'。很難知道什麼是。建議你在每個成功處理程序中以不同的方式命名它們,同時調試和可讀性,但使差異更明顯 – charlietfl 2015-01-20 22:08:35

+0

感謝,我已編輯它 – ChatCloud 2015-01-20 22:25:12

+1

我建議一個url返回json,另一個返回html,或者需要設置'dataType'以匹配發送的內容。拋出什麼錯誤? – charlietfl 2015-01-20 22:37:03

回答

0

沒有足夠的信譽評論因此增加一個答案 這裏正在擴大使用的數據類型charlietfl評論。

我做了它的工作使用的dataType = 「JSON」。根據jQuery文檔,Json必須嚴格格式化。

jQuery.ajax({ 
    url: 'some_other_url', 
    success: function(some_other_response){ 
     jQuery.ajax({ 
     url: 'request_url', 
     dataType:"json", 
     success: function(response){ 
      alert(response.status); //there is nothing 
      alert(response);   //I see 'thestuff' which is expected in 'html' 
     } 
     }) 
    } 
}); 

request_url應該在所有的變量`respose0`返回類似這樣(注意,應使用引號,而不是單引號)

{"status":"s","html":"some stuff"}