开发者

json get not access returning as undefined

I am currently doing the following with some jQuery,

    $(".template-choice").live("click", function(event){$.post($(this).attr('href'), function(data) {
                console.log(data);
            $('.textarea textarea').val(data.letter_template_content);
     });
     event.preventDefault();
});

As you can see I trying to insert the JSON var into a textarea however my data.letter_template_content is returning as undefined below is my JSON object,

{"template":[{"letter_template_id":"1","letter_template_name":"Change of address","letter_template_content":"<p style=\"font-weight: bold;\">[Your Name] <\/p><p style=\"font-weight: bold;\">[Address] <\/p><p style=\"font-weight: bold;\">[Town, City] <\/p><p><span style=\"font-weight: bold;\">[Post Code] <\/span><br><\/p><p><br><\/p><p> <\/p><p>[Recipient Name] <\/p><p>[Job Title] <\/p><p>[Company Name] <\/p><p>[Address] <\/p><p>[Town, City] <\/p><p>[ Post Code] <\/p><p> <\/p><p> <\/p><p>Dear <span>[Recipient Name]<\/span>: <br><\/p><p><br><\/p><p> <\/p><p>This letter is to inform you that I have a new mailing address. Please update your records to replace my previous address: <br><\/p><p><br><\/p><p>[Previous Address] <\/p><p>[Town, City] <\/p><p><span>[Post Code]<\/span> <\/p><p> <\/p><p>with the following new address: <br><\/p><p><br><\/p><p>[New Address] <\/p><p>[Town, City] <\/p><p><span>[Post Code] <\/span><br><\/p><p><br><\/p><p> <\/p><p>Thank you for your attention to this matter. <br><\/p><p><br><\/p><p>[Your Name] <\/p>","letter_template_create开发者_运维百科d":"2011-09-09 15:59:51"}]}

how do i access the letter_template_name key?


That's because data.letter_template_content doesn't exist.

Try:

data.template[0].letter_template_id
data.template[0].letter_template_name
data.template[0].letter_template_content

Your (very nicely formatted JSON, may I add), would look like this in a nicely formatted world:

{
    template: [
        {
            letter_template_id: 1,
            letter_template_name: "Change of address",
            letter_template_content: "lots of html",
        },...
    ]
}


this one is correct

data.template[0].letter_template_content
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜