Honey Goyal
Honey Goyal

Reputation: 445

getting undefined in javascript while accessing json array

Getting problem to access json array.

this my javascript

function getCustomerInfo(){
    $.ajax({
    dataType: 'json',
    url:'https://host:8443/xxxxxx/xxx',
    type: 'POST',
        data: {requestor_email: '[email protected]'},
    success:function(data){
        alert(JSON.stringify(data)); //first alert
        alert(data.outputMap.customerName); //second alert
        alert(data.outputMap.emailId); //third alert
        alert(data.outputMap.orders); //fourth alert
        alert(data.outputMap.orders[0]); //fifth alert
    },
    error:function(data){
        alert(JSON.stringify(data));
        }
   });
}

My first alert prints the json response i.e

`

{
 "targetRequestUri":"/getCustomerInfo",
 "javax.servlet.request.key_size":128,
 "outputMap":
            {
             "emailId":"[email protected]",
             "orders ":[
                       {
                        "orderId":"ST210340",
                        "orderDate":"2013-04-24 12:42:54.187",
                        "orderStatus":"ORDER_COMPLETED",
                        "totalMoney":1
                       }
                       ],
             "partyId ":"10810",
             "customerName":"honey goyal"
            },
 "_FORWARDED_FROM_SERVLET_":true,
 "javax.servlet.request.ssl_session":"519ee62b8656107fa3ac262c2ac8f86e0348933dc980e0078eca2fd638b55303",
 "javax.servlet.request.ssl_session_id":"519ee62b8656107fa3ac262c2ac8f86e0348933dc980e0078eca2fd638b55303",
 "_SERVER_ROOT_URL_":"https://host:8443",
 "javax.servlet.request.cipher_suite":"TLS_ECDHE_RSA_WITH_RC4_128_SHA",
 "thisRequestUri":"json"
}`

Second alert prints the correct customerName i.e honey goyal

Third alert prints the correct emailId i.e [email protected]

Forth alert prints the undefined, this is the problem, it should prints [object Object]s.

Fifth alert also responds the undefined.

Thanks in advance.

Upvotes: 0

Views: 607

Answers (1)

techfoobar
techfoobar

Reputation: 66663

The correct key name for orders is "orders " (with a space at the end) as per the alerted JSON string.

Try changing the fourth alert to: alert(data.outputMap['orders ']);

Upvotes: 2

Related Questions