What is wrong with jquery Ajax?
我正在使用Jquery ajax从数据库接收信息。我测试服务器端方法。它工作正常。当页面中的调用是错误时json:
消息:"无效的JSON原语:}。"
堆栈跟踪:
"在System.Web.Script.Serialization.JavaScriptObjectDe serializer.BasicDeserialize(String input,Int32 depthLimit,JavaScriptSerializer serializer)
System.Web.Script.Serialization.JavaScriptSerializ er.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,Int32 depthLimit)的System.Web.Script.Serialization.JavaScriptSerializ er.Deserialize [T](字符串输入)中的depthLimit,JavaScriptSerializer序列化程序)在System.Web.Script.Script.Script.Script.Services上System.Web.Script.Script.Services.RestHandler.GetRawParam(WebServiceMethodData methodData,HttpContext context)的System.Web.Script.Services.RestHandler.GetRawParam sFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化程序)中。 RestHandler.ExecuteWebS erviceCall(HttpContext context,WebServiceMethodData methodData)"
ExceptionType:"System.ArgumentException"
方法sode是:
1 2 3 4 5 6 | [WebMethod] public static ParentDataT_book.OptionBook[] SearchNameBook(string nameBook, int state) { var bookBusiness = new T_bookBusiness(); return bookBusiness.GetList(nameBook, (byte)0).ToArray(); } |
jquery ajax代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | function searchBook() { j.ajax({ type:"POST", url:"Default.aspx/SearchNameBook", //??? ???? ? ??? data:"{'nameBook': '" + j('#ContentPlaceHolder1_txt_nameBook').val() +"', 'state': '" + j('#states').val() - 1 +"'}", contentType:"application/json; charset=utf-8", dataType:"json", success: function (msg) { var delay = function () { AjaxSucceededSearch(msg); }; setTimeout(delay, 1500); //remove this }, error: AjaxFailedSearch }); //}); }; // }); function AjaxSucceededSearch(data) { if (result.d != null) { alert("data is found!!!"); } else if (result.d == null) { alert("data is not found!!!"); } } function AjaxFailedSearch(result) { alert(result.status + ' ' + result.statusText); } |
朋友你知道问题是什么吗?
最终的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | function searchBook() { var stateVal = (parseInt(j('#states').val()) - 1); var nameBookkVal = j('#ContentPlaceHolder1_txt_nameBook').val(); j.ajax({ type:"POST", url:"Default.aspx/SearchNameBook", //??? ???? ? ??? data:"{'nameBook':'"+nameBookkVal+"','state':'"+stateVal+"'}", contentType:"application/json; charset=utf-8", dataType:"json", success: function (result) { var delay = function () { AjaxSucceededSearch(result); }; setTimeout(delay, 1500); //remove this }, error: AjaxFailedSearch() }); }; function AjaxSucceededSearch(result) { if (result.d != null) { alert("data is found!!!"); } else if (result.d == false) { alert("data is not found!!!"); } } function AjaxFailedSearch(jqXhr, textStates , errorThrown) { // alert(errorThrown + ' ' + textStates); } |
注意:我删除了数据行中的空格字符。现在
这段代码工作正常。谢谢你的回答。