2011-04-20 46 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);