简体   繁体   中英

Javascript alert from JSON response

I'm trying to build an app that gets data from server with getJSON and then alerts the row. This is the code I use but it doesn't show the row from JSON, it just says "undefined". What could be the reason for this?

$(document).ready(function(){

  $("button").click(function(){
    $.getJSON("MyURL/test.php",function(result){
      $.each(result, function(i, field){
        alert(field.MESSAGE);
      });
    });
  });
});

This is the JSON response:

{"key":[{"message":"test"}]}

result isn't an array. result.key is an array. Also your field's property is message , not MESSAGE :

$.each(result.key, function(i, field){
    alert(field.message);
});

访问json中的对象区分大小写

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