简体   繁体   中英

How to retrieve data from a jquery ajax response that is a list of maps?

So I have a response from an ajax call that is a list of maps . I need to iterate over the response, and from each map in the list, fetch a specific value using the key "schoolName". My code will make clear what I want to do:-

 success:function(response){
    response.each(function(index,value){
        console.log(value.get("schoolName"))
    })
}

This throws me an error :- Menu.html:33 Uncaught TypeError: response.each is not a function

How do I get the "schoolName" value from all the maps in the response list?

Try with $.each()

$.each(response, function (index, value) {
   console.log(value.schoolName)            
});

Source: jQuery Page

Since it's an object, you can use $.each . Also just use dot notation to access schoolName :

success:function(response){
    $.each(response, function(index,value){
        console.log(value.schoolName)
    })
}

First, you have to decode responde. maybe it is in Json?

jQuery.parseJSON(response);

Then, use jQuery.each correctly:

success:function(response){
    jQuery.each(jQuery.parseJSON(response), function(index, value){
        console.log(value.get("schoolName"))
    })
}

To be more accurate, a response template might help

not sure what your response is like, but if schoolName is a key of an object and your response is an array of objects, you can do:

(response) => {
   for (let i of response) {
      console.log(i.schoolName);
   }
}

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