无法访问 JSON
我有这个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
I have this ajax request,
$.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;
I get the following in my console,
[{"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"}]
But when I do something like,
alert(html.productTitle)
all I get is undefined?
What am I doing wrong?j
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是因为你的
html
变量是一个数组吗?难道你不必做...Is it because your
html
variable is an array? Wouldn't you have to do...尝试 html[0].productTitle,我已经遇到过这个问题几次了。
Try html[0].productTitle, I have run into this problem a few times.
尝试使用 Jquery 的 JSON ajax 函数而不是 $.ajax,它会为您解析 JSON。
http://api.jquery.com/jQuery.getJSON/
Try using Jquery's JSON ajax function instead of the $.ajax it will parse the JSON for you.
http://api.jquery.com/jQuery.getJSON/
尝试做这样的事情:
警报(html.d); // 这将显示 ajax 调用的结果
我不知道使用属性“d”的原因是什么,但如果您可以查看结果,请使用调试工具查看哪些数据返回您的 ajax 调用。
如果要将 JSON 字符串转换为对象,可以使用以下代码:
var respuesta = jQuery.parseJSON(html.d);
try doing something like this:
alert(html.d); // this will show the result of your ajax call
i don't know what the reason is to use the property 'd' but if you can take a look at your result, use a debug tool to see what data is getting back your ajax call.
if you want convert your JSON string to and object, you can do with the following code:
var respuesta = jQuery.parseJSON(html.d);