无法访问 JSON

发布于 2024-11-02 06:26:39 字数 1369 浏览 1 评论 0原文

我有这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

篱下浅笙歌 2024-11-09 06:26:40

是因为你的 html 变量是一个数组吗?难道你不必做...

alert(html[0].productTitle);

Is it because your html variable is an array? Wouldn't you have to do...

alert(html[0].productTitle);
尾戒 2024-11-09 06:26:40

尝试 html[0].productTitle,我已经遇到过这个问题几次了。

Try html[0].productTitle, I have run into this problem a few times.

末が日狂欢 2024-11-09 06:26:40

尝试使用 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/

维持三分热 2024-11-09 06:26:40

尝试做这样的事情:


警报(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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文