Skip to content Skip to sidebar Skip to footer

JQuery.ajax "data" Parameter Syntax

I am trying to pass the contents of a javascript variable to the server for processing. I can pass static strings no problem but when I pass a variable containing a string, the We

Solution 1:

You don't have to pass it as a string. Since ID is a javascript variable you have to pass its value. When you pass data as "{sendData: ID}" it will not pass the value of ID.

Try this

data: { sendData: ID }

Solution 2:

You were sending a string instead of an object ("{sendData: ID}" instead of {sendData: ID}). And the data you were sending wasn't JSON. So remove the contentType line and change the data line. You should re-write this as:

$.ajax({
    type: "post",
    url: "Playground.aspx/childBind",
    data: {sendData: ID},
    dataType: "json",
    success: function (result) { alert("successful!" + result.d); }
})

You can also write this, if you want to send JSON:

$.ajax({
    type: "post",
    url: "Playground.aspx/childBind",
    data: $.getJSON({sendData: ID}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (result) { alert("successful!" + result.d); }
})

Solution 3:

Since you are using jQuery run these tests for the answer:

var ID = 45;

var x = "{sendData: ID}";

alert(jQuery.isPlainObject(x));  // false

var y = { sendData: ID};

alert(jQuery.isPlainObject(y)); // true

Here is the jsFiddle:

http://jsfiddle.net/3ugKE/


Solution 4:

You are passing in 'ID' as a string rather than a variable. All you need to do is remove the quotes around your data object.

Further reading on JSON and javascript objects:

http://www.json.org/

http://www.w3schools.com/js/js_objects.asp


Solution 5:

you can use this code :

$.ajax({
        type: "post",
        url: "Playground.aspx/childBind",
        data: JSON.stringify({sendData: ID}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) { alert("successful!" + result.d); }
    })

Post a Comment for "JQuery.ajax "data" Parameter Syntax"