1

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?

5
  • (var ajaxData = { key : [ "data1":data1, "data2":data2 ]}; && data: ajaxData) || data: { key : [ "data1":data1, "data2":data2 ]} Commented Apr 14, 2017 at 13:32
  • @AlonEitan thanks... Commented Apr 14, 2017 at 13:35
  • that structure is invalid ...arrays don't have properties...objects do Commented Apr 14, 2017 at 13:41
  • @charlietfl Oh right! I hope it's just the OP forgot to include a pair of curly brackets or two in the question Commented Apr 14, 2017 at 13:43
  • oh yes, curly braces. Commented Apr 15, 2017 at 6:56

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the solution. i had the similar approach. my intension was to assign the data to a key, it just makes it lot easier to retrieve the data later on. Thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.