Reputation: 3314
I am using $.ajax
to send request to C# code in Asp.Net.Each time i am getting the error in the response(checked in Firebug) like:
{"Message":"Invalid JSON primitive: EmailAddress.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
If i remove data:{}
parameter from the $.ajax
then it is fine.I think there is some problem with my way of sending data to the server.
My Client side code is: function send_req() {
$.ajax({
url: "Demo.aspx/Demo_Method",
contentType: "application/json; charset=UTF-8",
type: "POST",
data: {"EmailAddress": "[email protected]"},
success: function (response) {
alert('Success' + response);
}
});
}
And Demo.aspx.cs page code is:
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod()]
public static void Demo_Method(string EmailAddress)
{
//Some code....
}
}
Upvotes: 1
Views: 195
Reputation: 4947
Remove quotation mark around EmailAddress
parameter:
$.ajax({
url: "Demo.aspx/Demo_Method",
contentType: "application/json; charset=UTF-8",
type: "POST",
data: {EmailAddress: "[email protected]"},
^^^^^^^^^^^^
success: function (response) {
alert('Success' + response);
}
});
Upvotes: 2
Reputation: 1
Try to pass the data as string, not as object.
example:
$.ajax({
url: "Demo.aspx/Demo_Method",
contentType: "application/json; charset=UTF-8",
type: "POST",
data: '{"EmailAddress": "[email protected]"}',
success: function (response) {
alert('Success' + response);
}
});
Upvotes: 0
Reputation: 5156
In my calls, I specify dataType: "json"
, and wrap the whole data
parameter in quotes.
e.g.
$.ajax({
url: "Demo.aspx/Demo_Method",
contentType: "application/json; charset=UTF-8",
type: "POST",
dataType: "json",
data: "{'EmailAddress':'[email protected]'}",
success: function (response) {
alert('Success' + response);
}
});
Upvotes: 0