Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

    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'

like image 324
bash- Avatar asked Dec 29 '25 06:12

bash-


1 Answers

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.

like image 165
Daniel Roseman Avatar answered Dec 30 '25 21:12

Daniel Roseman