以下是我对服务器的AJAX请求。我已经从他们的官方网站here了解了如何做到这一点;但是,我并不完全了解如何正确传递data
。
$('#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"
}
});
它不断返回以下错误:
{"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"}
我试图从服务器端函数和AJAX调用中删除参数,它工作正常。但是,我不明白如何使用AJAX。
更新:所以,我在没有JSON.stringify()的情况下使用了它,它给了我以下内容:
{"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"}
更新以下是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);
}
FillTable方法基本上会填写表格。该方法在转换为Web方法之前工作正常。返回普通的JSON。
更新:在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"}
答案 0 :(得分:2)
将"data": JSON.stringify({ businessClass: "true" }),
更改为"data": { "businessClass": "true" },
并尝试