Hi,
Anyone have an idea what I am doing wrong here. I'm trying to
POST JSON objects to ASP.Net WebMethod using jQuery AJAX.
Although I'm getting the objects on server side but not the way I
wanted. I wanted customer object to be a simple object so I can access like normal instances e.g. customer.Name, but I can't because it's getting there as dictionary object.
EDIT:: Why JSON is getting on server side as Dictionary object for c# dynamic type?
Here's the quick watch screen-cast;
Here's javascript and server side code.
function SaveCustomer() {
var funcParams = JSON.stringify({
customer: {
Name: "Name of Customer",
Title: "President"
},
address: {
Street: "Street",
City: "",
Zip: ""
}
});
// I tried with the following json parameters but still same result.
var funcParams = "{\"customer\":" + JSON.stringify({ Name: "Name of
Customer", Title: "President" }) + ",\"address\":" +
JSON.stringify({ Street: "Street", City: "", Zip: "" }) + "}";
}
$.ajax({ type: "POST", contentType: "application/json; charset=utf-8",
dataType: "json", url: "aspxPage.aspx/SaveCustomer", data: funcParams,
success: function(){ }, error: function(e){alert("Error occured"+e);}
})
[WebMethod(EnableSession = true)]
public static string SaveCustomer(dynamic customer, dynamic address)
{
if(!string.IsNullOrEmpty(customer.Name) &&
!string.IsNullOrEmpty(customer.Title)....)
{
//app logic
}
}