Atul Kumar
Atul Kumar

Reputation: 159

Parse nested json into ajax

I have sample json structured as,

{ key : { "data1":data1, "data2":data2 }}

I want it to be parsed into the 'data',

$.ajax({
    type: 'post',
    url: 'url',
    data: <--- here ,
    success: function() { 
        *****
    }
});

How do I do it?

Upvotes: 1

Views: 2841

Answers (1)

selvarajmas
selvarajmas

Reputation: 1643

Try to use JSON.stringify

var data= { "key" : {"data1":"data1", "data2":"data2" }};
new Request.JSON({
    url: '/echo/json/',
    type: "POST",
    data: JSON.stringify(data),
    contentType: "application/json",
    onSuccess: function(res) {
        document.write(data.key.data1);
        console.log(data.key.data1);
    }
}).send();

Here is the working jsfiddle: http://jsfiddle.net/dK5DL/87/

Upvotes: 1

Related Questions