json.parse给出未知的语法错误:意外的令牌(Django json serialized queryset) [英] json.parse gives Uncaught SyntaxError: Unexpected Token (Django json serialized queryset)

查看:284
本文介绍了json.parse给出未知的语法错误:意外的令牌(Django json serialized queryset)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试解析json数据时,我遇到错误 Uncught SyntaxError:Unexpected Token

I am experiencing the error Uncaught SyntaxError: Unexpected Token when trying to parse the json data

这是我的ajax代码(json2.js):

This is my ajax code (json2.js):

       $.ajax({
            type: 'POST',
            url: '/best_choose/invoice/item_search.json/',
            data: jsonQuery,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',

            success: function(data){
                    var parsed = JSON.parse(data);
                    //do stuff
             }});

我的观点:

    json_serializer = serializers.get_serializer('json')()
    serialized_q = json_serializer.serialize(queryset, ensure_ascii=False)

    return HttpResponse(
                serialized_q, mimetype='application/json'
            )

从debug serialized_q是一个unicode字符串,包含有效的json u'valid_json'

from debug serialized_q is a unicode string containing valid json u'valid_json'

推荐答案

设置 dataType json ,jQuery解析你的数据。所以你不需要把它通过 JSON.parse ,你可以将数据作为一个正常的Javascript对象。

When you set dataType to json, jQuery parses the data for you. So you don't need to put it through JSON.parse, you can just refer to data as a normal Javascript object.

这篇关于json.parse给出未知的语法错误:意外的令牌(Django json serialized queryset)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆