jQuery AJAX call to pass JSON to C# Method

(by AXDS)

Note: The C# method parameter needs to match the JSON key name.

C#:

[WebMethod]
public static void doSomethingCool(Object jsonDataName)
{
Debug.WriteLine(jsonDataName);
}

SCRIPT:

var jsonInnerData = {key1:value1,key2:value2};
var methodToCall = "webPage.aspx/doSomethingCool";
var jsonDataObject = JSON.Stringify({jsonDataName : jsonInnerData});

$.ajax({
type: 'POST',
url: methodToCall,
data: jsonDataObject,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function ()
{
alert('doSomethingCool executed.');
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert("Status: " + textStatus); alert("Error: " + XMLHttpRequest.responseText);
}
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s