1

So below is my AJAX request to the server. I already read how to do it from their official website here; however, I do not fully understand how to pass in the data properly.

        $('#MainContentPlaceHolder_business_return_flights').dataTable({
            "ajax": {
                "url": "Browse.aspx/GetReturnFlights",
                "type": "POST",
                "data": JSON.stringify({ businessClass: "true" }),
                "contentType": "application/json; charset=utf-8",
                "dataType": "json"
            }
        });

It keeps on returning the following error:

{"Message":"Invalid JSON primitive: %7B\u00261=%22\u00262=b\u00263=u\u00264=s\u00265=i\u00266=n\u00267=e\u00268=s\u00269=s\u002610=C\u002611=l\u002612=a\u002613=s\u002614=s\u002615=%22\u002616=%3A\u002617=%22\u002618=t\u002619=r\u002620=u\u002621=e\u002622=%22\u002623=%7D.","StackTrace":"   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"}

I have tried to remove the parameters from the server side function as well as the AJAX call, and it works fine. I don't understand how I can use the AJAX with it, though.

Update: So, I have used it without JSON.stringify() and it gives me the following:

{"Message":"Invalid JSON primitive: businessClass.","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"}

Update Here is the method in the Code Behind.

    [WebMethod]
public static string GetReturnFlights(string businessClass)
{
    listRows = new List<List<string>>();
    business = new Table();
    economy = new Table();

    FillTable(economy, business, scheduledFlights.List);

    if (businessClass.Equals("true"))
    {
        foreach (TableRow row in business.Rows)
        {
            listRow = new List<string>();

            foreach (TableCell cell in row.Cells)
            {
                listRow.Add(cell.Text);
            }

            listRows.Add(listRow);
        }
    }
    else
    {
        foreach (TableRow row in economy.Rows)
        {
            listRow = new List<string>();

            foreach (TableCell cell in row.Cells)
            {
                listRow.Add(cell.Text);
            }

            listRows.Add(listRow);
        }
    }

    field = new Dictionary<string, object>() { { "draw", "1" }, { "recordsTotal", economy.Rows.Count.ToString() }, { "recordsFiltered", economy.Rows.Count.ToString() }, { "data", listRows } };

    return new JavaScriptSerializer().Serialize(field);
}

The FillTable method would basically fill in the tables. The method works fine before it was turned into a web method. Returns normal JSON.

Update: Result for adding double quotes around businessClass

{"Message":"Invalid JSON primitive: businessClass.","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"}
1
  • Try removing JSON.stringify Commented Jun 3, 2014 at 5:14

1 Answer 1

2

Change "data": JSON.stringify({ businessClass: "true" }), to "data": { "businessClass": "true" }, and try it

Sign up to request clarification or add additional context in comments.

6 Comments

Did you debugged by using breakpoint what you get in the businessClass?
@yaharga i have edited the answer provide double quotes around businessClass.
I don't believe the server code is being run in the first place, as the error occurs within the AJAX rather than the server side function.
I think the issue is with you server code not in ajax, please check by placing the breakpoint in the webmethod.
Placed breakpoints in the method. Never gets run in the first place. Program runs fine.
|

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.