1

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....

    }
}
2
  • Do you have an overloaded method "void Demo_Method(int something)"? Commented Mar 12, 2013 at 8:10
  • No only these two methods are there:void Page_Loda() and Void Demo_Method(string EmailAddress) Commented Mar 12, 2013 at 9:09

3 Answers 3

2

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);
   }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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);
        }
    });

Comments

0

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);
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.