This sample will show how to Send multiple value to Server using JSON.
Step 1 : Create WebService Method.
[WebMethod]
public string GetData(string Data)
{
System.Web.Script.Serialization.JavaScriptSerializer JSON = new System.Web.Script.Serialization.JavaScriptSerializer();
Object obj = JSON.DeserializeObject(Data);
Hashtable ht = new Hashtable();
foreach (KeyValuePair d in (Dictionary)obj)
{
ht.Add(d.Key, d.Value);
}
return "GetData successfully.";
}
Step 2 : Create JavaScript function using jQuery.ajax
Declare Array type in JavaScript and serialize to JSON Data and pass value to WebService.
function SendData() {
var arg = {};
arg["Data1"] = "String1";
arg["Data2"] = 950;
arg["Data3"] = "String2";
arg = Sys.Serialization.JavaScriptSerializer.serialize(arg);
$.ajax({
url: "WebService.asmx/GetData",
data: "{ 'Data': '" + arg + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
alert(data.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
}
WebService can get JSON data that send from jQuery.ajax
enjoy ZomDev
This is throwing error in client side saying
ReplyDelete"" arg = Sys.Serialization.JavaScriptSerializer.serialize(arg); ""
Sys is undefined
hi
DeleteSys is defined by ASP:ScriptManager
Please add ScriptManager to your *.aspx page