1

I am trying to read the response of the server that gives me the following JSON:

{"result": "Success", "message": "[{\"model\": \"mysite.userprofile\", \"pk\": 1}, {\"model\": \"mysite.userprofile\", \"pk\": 4}]"}

When I try to read it I get as is a String. If I try to parse it is not possible as it gives me

Unexpected token o in JSON at position 1

which means that is a Json already.

So this code:

$.ajax({
            type: 'POST',
            contentType: 'application/json',
            dataType: 'json',
            url: '/search/?city=&radius=',
            success: function (data) {
                //data = JSON.parse(data);
                var ul = document.getElementById("contact-list");
                console.log("JSON?  "+data.message[0]); // Print [ which is the first char

                for (var x = 0; x < data.message.length; x++){
                     // iterate throw every character in the message
                }

So inside the properties ('result' and 'message') it appears as there is a String instead of a Json

And here is the server side:

def search_profiles_by_loc(request):
    if request.method == "POST" and request.is_ajax():
        city = request.POST.get('city', False)
        all_profiles = UserProfile.objects.all()
        response_data = {}
        response_data['result'] = 'Success'
        response_data['message'] = serializers.serialize('json', all_profiles)
        return HttpResponse(JsonResponse(response_data), content_type="application/json")

I tried a lot of things nothing work

6
  • inside message there is a json string you have to parse that then only you will be able to use it Commented Jul 6, 2017 at 10:30
  • 2
    When you use dataType: 'json' there is no need to use JSON.parse() Commented Jul 6, 2017 at 10:31
  • 2
    Either use JSON.parse() on the string. Or send it as an array, without enclosing with " Commented Jul 6, 2017 at 10:31
  • A JSON (-string) is always a string! There is no such thing as a JSON-object. There are only objects. I'd guess that this misconception probably lead you to manually JSON-encode the message in the backend before you add it to the response. You should fix that mistake on the server rather than trying to work around this in the client. Commented Jul 6, 2017 at 10:38
  • I see @Thomas I added the server side. If I do not ecode the message I do not get it well Commented Jul 6, 2017 at 11:00

2 Answers 2

2

As harish-reddy said, the dataType property sets the behaviour to what you expect the response to be. That said the "data" argument of the success function is already a JavaScript object.

Refer to http://api.jquery.com/jQuery.ajax/

As arpit-solanki mentioned the message property inside the response object is not an array, but is interpreted as a string (notice the starting '"'), therefor you either change the server side to send you an actual array or you must JSON.parse(data.message) to obtain its content.

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

1 Comment

I did it before, but I misunderstood as it said undefined, but yes I can access the object inside. Thanks!
0

The response you are receiving is already executed by JSON.stringify.Either you must use JSON.parse or change the logic at the server end

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.