简体   繁体   中英

Can not get value from JSON object

I'm having trouble getting the data value from JSON object. Here is my code:

var ab_id = $( "#ab_id" ).val();
$.ajax({
    type: 'GET',
    contentType: 'application/json',
    url: 'edit_account.php',
    data: {ab_id:ab_id, u_id:u_id},
    success: function(data) 
    {
        alert(data.ab_name);        
    },

});

When I do alert(data) , I got the actual data like this:

{    
     "ab_id":"7",
     "ab_name":"Lily's Storage Address",
     "ab_ship_name":"LIly C\/O SELF STORAGE",
     "ab_addr_1":"C\/O Lily",
     "ab_addr_2":"16 PIUMA AVENUE, UNIT #2",
     "ab_city":"CERI",          
     "ab_state":"CA",
     "ab_postal":"90700",
     "ab_phone":null,
     "ab_default":"0",
     "ab_is_storage":"1"
}

However, when I retrieve the data value by using data.ab_name , it returns undefined.

Am I missing something here?

Parse your string to a object

data = JSON.parse(data);
 alert(data.ab_name);

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