繁体   English   中英

如何从jquery ajax响应中检索地图列表中的数据?

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

因此,我收到了来自ajax调用的响应,该调用是一个地图列表 我需要遍历响应,并使用键“ schoolName”从列表中的每个映射中获取特定值。 我的代码将阐明我想做什么:

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

这给我抛出一个错误: -Menu.html:33 Uncaught TypeError:response.each不是一个函数

如何从响应列表中的所有地图中获取“ schoolName”值?

尝试$ .each()

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

资料来源: jQuery Page

由于它是一个对象,因此可以使用$.each 也只需使用点符号来访问schoolName

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

首先,您必须解码响应者。 也许是在杰森?

jQuery.parseJSON(response);

然后,正确使用jQuery.each

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

更准确地说,响应模板可能会有所帮助

不确定您的响应是什么样,但是如果schoolName是对象的键,而您的响应是对象的数组,则可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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