简体   繁体   中英

Print key and value pairs of dictionary separately in ajax resposne

This is my dictionary

[{'entity': 'first entity', 'place': 'first palce'}, {'entity': 'second entity', 'place': 'second place'}]

I want to pass it to Html template file and I am passing as below

return(dict(docs=docs))

How can I iterate through Ajax to print key, value separately??

I tried this, but not working

 function f(){
 $(document).ready(function(){
 $.get('ajx', function(response){ 

     $.each(response, function(key, value) {

        alert(key+' '+ value);
         });

     });
 });
}

I tried to use jQuery.parseJson() but not working!!!

In order to enumerate all the nested keys and values from that, you should do this:

$.each(response.docs, function(index, element) {
  $.each(element, function(key, value) {
    console.log("At index", index, "key:", key, "value:", value);
  });
});

(I prefer console.log to alert - too much noise.)

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