Reputation: 1281
I have had this problem for couple of days now, My response from the Django is
di = { 0 : 'bat' , 1 : 'ball' }
js = json.dumps(js , indent=4)
print js
"""
{
"0": "bat",
"1": "ball"
}
"""
return HttpResponse(js)
now in my Ajax function:
$.ajax({
type: "POST",
url: "/some/url",
data: {
'name': "myname",
csrfmiddlewaretoken: '{{ csrf_token }}'
},
dataType: "json",
success: function (data) {
alert(data)
var to_parse = $.parseJSON(data);
alert(to_parse)
}
});
return false
}
I expect in alert(data)
the json value, but instead it get this: [object object]
Where am I going wrong?
Upvotes: 0
Views: 76
Reputation:
Use console.log(data);
This will display the contents to the console, you can view the console in Google Chrome by pressing F12 and clicking "Console"
Upvotes: 1
Reputation: 332
Because you are telling Jquery that your data is json via the "dataType: "json"" option, jquery goes ahead and turns your text into a javascript object. So when you do alert(data) you get the object not the text. You should either remove the line where you parese the data to json yourself or remove dataType:json from your jquery options.
Just to answer you question explicitly, replace alert(data) with alert(JSON.stringify(data))
Upvotes: 1