Reputation: 1547
I need to process two lists in Ajax
success function. The below simple code works well for a single list but doesn't work with 2 lists. How can we process 2 lists separately in a success function
.
jQuery Ajax
success: function(data) {
$.each(data, function() {
$.each(this, function(k, v) {
//do something with v
});
});
}
views.py
lst1 = [1, 2, 3, 4, 5]
lst2 = ['a', 'b', 'c', 'd', 'e']
context = {
'labels' : lst1,
'sk_labels': lst2
}
return HttpResponse(json.dumps(context), content_type='application/json')
Upvotes: 0
Views: 263
Reputation: 14927
Refer to jQuery.each()
's documentation. Try the following:
success: function(data) {
$.each(data, function(contextKey, values) {
$.each(values, function(index, value) {
// do something with value
});
});
}
Upvotes: 0
Reputation: 36
I had almost the same problem. Solution was pretty simple.
Try using in first Jquery function call. It helped me parsing data correctly.
JSON.parse(data);
success: function(data) {
$.each(JSON.parse(data), function() {
$.each(this, function(k, v) {
//do something with v
});
});}
Upvotes: 1