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"}
将"data": JSON.stringify({ businessClass: "true" }),
更改为"data": { "businessClass": "true" },
然后尝试
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.