开发者

json.parse gives Uncaught SyntaxError: Unexpected Token (Django json serialized queryset)

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

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
             }});

my view:

  开发者_如何学Python  json_serializer = serializers.get_serializer('json')()
    serialized_q = json_serializer.serialize(queryset, ensure_ascii=False)

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

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜