【问题标题】:Problem with getting a JSON string获取 JSON 字符串的问题
【发布时间】:2010-11-12 20:39:37
【问题描述】:

这是我的代码,它通过 AJAX 发送表单,然后向用户显示一条消息

var postValues =   {
                name: $($nameObject).val(), 
                email: $($emailObject).val(),
                message: $($messageObject).val(),
                form: $(this).find('input[name=form]').val()
            };


            var form = this;


            $.post(config.basePath + 'contact/', postValues, function(data) {

                // get json here and make sure it sent



                 console.log('done request!');

                console.log(data.success);

                var $statusObject = $(form).find('.status-message');


                if (data.success) {

                    console.log(data.message);

                    $statusObject.removeClass('failed').text(data.message);

                    setTimeout(function() {

                        $statusObject.fadeOut(1500).remove();

                        $(form).find('input[type=text], textarea').val('');

                        $(form).find('button[type=submit]').removeAttr('disabled').parent('div').removeClass('sending');


                    }, 1500);

                } else { // error with ajax

                    $statusObject.addClass('failed').text(data.message);
                    $(form).find('button[type=submit]').removeAttr('disabled');

                }




              }, 'json');



            return false;


        });

Firebug 说返回的 JSON 是

{"成功":true,"消息":"已发送 成功”}

我可以轻松获得data.success 的值,但每当我尝试访问data.message 时,它都会设置为“未定义”。

但是,当我这样做时

console.log(data)

我在 Firebug 中得到了正确的输出:

对象成功=true 消息=发送成功

这听起来像是什么原因?这让我有点发疯!

提前感谢您的帮助。

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    您正在尝试访问 data.message,但您的 JSON 包含 data.messsage。用三个 esses。

    【讨论】:

    • 哇!我知道它一定是那样的愚蠢。谢谢混乱:)
    【解决方案2】:

    您在 Firebug 中的返回字符串似乎是我sssage...注意多余的 s。

    我认为这可能是你的问题;)

    【讨论】:

    • 我给你接受的答案是因为你的代表最少:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2013-02-18
    相关资源
    最近更新 更多