3

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'

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.