我有这个ajax请求,
$.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)
我得到的是未定义的?
我做错了什么?j
答案 0 :(得分:6)
是因为你的html
变量是一个数组吗?你不必这么做......
alert(html[0].productTitle);
答案 1 :(得分:2)
试试html [0] .productTitle,我已经遇到过几次这个问题了。
答案 2 :(得分:0)
尝试使用Jquery的JSON ajax函数而不是$ .ajax,它将为您解析JSON。
答案 3 :(得分:0)
尝试做这样的事情:
alert(html.d); // this will show the result of your ajax call
我不知道使用属性'd'的原因是什么,但如果您可以查看结果,请使用调试工具查看哪些数据正在回复您的ajax调用。
如果要将JSON字符串转换为对象,可以使用以下代码:
var respuesta = jQuery.parseJSON(html.d);