user1281022
user1281022

Reputation:

JQuery POST With JSON

This is my json object

{
    "button": {
        "name": "test",
        "price_string": "1.23",
        "price_currency_iso": "USD",
        "custom": "Order123",
        "description": "Sample description",
        "type": "buy_now",
        "style": "custom_large"
    }
}

how can i write it in JQUERY? I Am trying to write it inside the data attribute (as is) - but it doesn't work.

i believe my problemis syntax.

 $.ajax({
        url: 'url',
        type: 'POST',
        data: ____
        ,"price_string": "1.23"}',
        error: function (msg) {
        alert( "error" + msg.toString());
        },
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'
    });

Upvotes: 2

Views: 76

Answers (1)

frenchie
frenchie

Reputation: 51937

You need to serialize the object to json, something like this:

var MyObject = {
    "button": {
        "name": "test",
        "price_string": "1.23",
        "price_currency_iso": "USD",
        "custom": "Order123",
        "description": "Sample description",
        "type": "buy_now",
        "style": "custom_large"
    }
};

MyObject = JSON.stringify(MyObject);

$.ajax({
        url: 'url',
        type: 'POST',
        data: MyObject, ...

Upvotes: 2

Related Questions