简体   繁体   中英

How to process data passed in success function in jQuery Ajax

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')

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
    });
});}

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
    });
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM