I've looked at other questions related to this one, but have yet to find something that answers my (first!) question:
I'm serializing a django view and sending it to the server with ajax (jquery). I receive the serialized data (tested it with alert and received the JSON data, 200 ok reponse), but am unable to get the following to work on success:
$('div#nextSet' + currentLetter).click(function(){
var output = '';
$.ajax({
url: 'path/to/django/view',
data: {'data':sentToServer},
datatype: 'json',
error: function(xhr_data) {
display_error();
},
success: function(data) {
$.each(data, function(i){
var firstName =data[i].fields.first_name;
var lastName = data[i].fields.last_name;
var portrait = data[i].fields.portrait;
var output = '<ul><li>' + firstName + ' ' + lastName + '</li>';
output += '<li><img src="' + portrait + '" /></li></ul>';
alert(output);
});
}
});
});
This code works in google chrome and firefox in the console (alert shows with sample html), but fails to run on the page. I'm getting the following error:
Uncaught TypeError: Cannot read property 'first_name' of undefined.
I've tried the console to see how to access javascript map/dictionary items, and am able to get a value when entering the following, replacing i with an actual number: data[number].fields.first_name The value is shown in the console and no error message appears.
This is a sample of the data returned from the django view for a single item:
var data = [{"pk": 8, "model": "app.model", "fields": {"portrait":
"this/is/the/photo/path.png", "first_name": "First", "last_name": "LastName"}}, ].
All of this is embedded in the success code, meaning nothing should be executed(?) unless the callback is available. So if data is present -- as alert(data) is working and I get a 200 ok response from the server -- and the code is working in console -- meaning the alert is processed and appears, is there something I'm overlooking?