【问题标题】:How to parse json data in jquery ajax success?如何在jquery ajax成功中解析json数据?
【发布时间】:2010-12-24 04:28:57
【问题描述】:
info = {
    'phone_number': '123456',
    'personal_detail': {'foo': foo, 'bar': bar},
    'is_active': 1,
    'document_detail': {'baz': baz, 'saz': saz},
    'is_admin': 1,
    'email': 'foo@bar.com'
}

return HttpResponse(
    simplejson.dumps({'success':'True', 'result':info}),
    mimetype='application/javascript')


if(data["success"] === "True") {
    alert(data[**here I want to display personal_detail and document_details**]);
}

我该怎么做?

【问题讨论】:

  • 尝试使用 jQuery 模板插件 (api.jquery.com/category/plugins/templates)。
  • 这是一篇令人困惑的帖子,因为您在同一代码块中有 python 和 javascript,并且缺少用于发出 ajax 请求的代码。您传递给 getJSON 的 url 是什么?您还可以在 json 中使用本机 bool 类型,在 python 中使用 True,在 javascript 中使用 true。

标签: jquery python ajax


【解决方案1】:

如果我正确理解问题,您可能正在寻找jquery parse json

【讨论】:

    【解决方案2】:
    $.getJSON('/ajax_url/',
        {
            'some_data': 'Some Value'
        },
        function(result){
            alert(result.personal_detail.foo);
        }
    );
    

    $.getJSON 使用您提供的 urlencoded 参数(第二个参数)向提供的 url(第一个参数)发送一个 GET 请求,并在成功时使用来自服务器的 JSON 解码结果调用成功函数(第三个参数) (result)。

    这是使用$.ajax和手动调用$.parseJSON等的快捷版本。

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多