2011-04-20 51 views
0

我有這樣的Ajax請求,着訪問JSON

$.ajax({ 
    url : '<?php echo current_url(); ?>', 
    data  : "txtKeywords="+$("#txtKeywords").val()+"&search=Search For Item", 
    type  : 'POST', 
    dataType : 'JSON', 
    success : function(html) 
    {  
     console.log(html);   
    } 
}); 
return false; 

我得到了我的控制檯下面,

[{"productId":"5","productTitle":"Small Brasserie Dining Table","productPath":"small-brasserie-dining-table\/","productRangeId":"6","productSecondaryRangeId":"0","productTypeId":"2","productRefNo":"0080","productShortDesc":"","productBestSeller":"0","productFeatured":"0","productIsSet":"0","productPrice":"275","productSavingType":"none","productSavingPrice":"0","productSavingMessage":"","productDimWidth":"90","productDimHeight":"74","productDimDepth":"90","productTechnical":"Powder coated aluminium frame with welded joints.","productTemplateId":"5","productMetadataTitle":"","productMetadataKeywords":"","productMetadataDescription":"","productBandingColour":"grey","productActualPrice":"275","rangeTitle":"Dining","parentRangeTitle":"Aegean","fullRangePath":"aegean\/dining\/","fullProductPath":"aegean\/dining\/small-brasserie-dining-table\/","hasImage":"0"}] 

但是,當我這樣做,

alert(html.productTitle) 

所有我get是不確定的?

我在做什麼錯【J

+0

我猜你需要解析字符串轉換成使用'JSON.parse' JSON對象。 – 2011-04-20 15:41:59

+0

通過current_url,你是否試圖對首次發起調用的頁面進行Ajax調用?這對我來說並不錯,但這也行不通! – bpeterson76 2011-04-20 16:03:46

回答

2

嘗試HTML [0] .productTitle,我也碰到這個問題了幾次。

6

是因爲你的html變量是一個數組嗎?難道你要做......

alert(html[0].productTitle); 
+0

那就是我在想什麼,很可能是他的問題...... – clamchoda 2011-04-20 17:23:33

0

嘗試做這樣的事情:

alert(html.d); // this will show the result of your ajax call

我不知道原因是使用屬性「d」什麼,但如果你可以看看你的結果,使用一個調試工具來查看哪些數據正在恢復您的ajax調用。

,如果你想你的JSON字符串轉換爲與目標,你可以用下面的代碼做:

var respuesta = jQuery.parseJSON(html.d);