jquery Ajax有什么问题?

时间:2015-07-04 08:19:24

标签: c# jquery ajax json

我正在使用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是:

[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代码是:

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 个答案:

答案 0 :(得分:0)

最终代码是:

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

    }

注意:我删除了数据行中的空格字符。现在 这段代码工作正常。谢谢你回答。