伙计们,我想从C#向jquery Ajax发送数据表。如果我发送字符串,它可以正常工作,如果我发送数据表,它给出下面我已发布的输出错误!



[WebMethod]
            public static DataTable getGuestByGuestIDFront(string guest_id)
            {

                DataTable dt621 = BAL.getGuestByGuestID(guest_id);
                return dt621;
            }


jQuery的

  < script type = "text/javascript" >

 function guest_search() {
     //      debugger;
     var guest_id = $('#guest_id').val();
     jQuery.ajax({
         url: 'Walkin6.aspx/getGuestByGuestIDFront',
         type: "POST",
         data: "{'guest_id':" + guest_id + "}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (data) {
             debugger;
             // TabItem2_click();
             console.log(data.d);
             //   alert("Booking ID " + data.d);
             // $("#message").html((data.d));

             //   $(this).prop("disabled", true);

         },
         error: function (result) {
             console.log('Failed' + result.responseText);
         }

     });
 } < /script>


这是输出!


  失败{“ Message”:“在系统\ Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember) \ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Sc ript.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable可枚举,StringBuilder System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat序列化格式,MemberInfo currentMember)的深度,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n在系统处。 Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(对象o,StringBuilder sb ,Int32深度,哈希表对象正在使用,序列化格式序列化为在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)中\ r \ n在System.Web.Script.Serialization中。 JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(对象o,StringBuilder sb,Int32深度,Hashtable objectsInUse ,位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)的r \ n,位于System.Web.Script。 Serialization.JavaScriptSerializer.SerializeValue(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)的r \ n在System.Web.Script.Serialization。 JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse ,位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n在System.Web.Script处的\ r \ n。 Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32 depth,Hashtable objectsInUs e,System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable可枚举,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象o,StringBuilder sb ,位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o,StringBuilder sb,Int32 depth,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n处的Hashtable objectsInIn,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat)\ r \ n位于System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o,StringBuilder sb,Int32深度,Hashtable objectsInUse,SerializationFormat serializationFormat,MemberInfo currentMember)\ r \ n n在System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象o,StringBuilder sb,Int32深度,哈希表对象正在使用,SerializationFormat序列化格式,MemberInfo currentMember)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,StringBuilder输出,System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,SerializationFormat serializationFormat)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj) )\ r \ n在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext上下文,WebServiceMethodData methodData,IDicti onary`2 rawParams)\ r \ n位于System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)“,” ExceptionType“:” System.InvalidOperationException“}


无论如何在jQuery Ajax中使用C#数据表!

最佳答案

C#代码

  [WebMethod]
        public static string getGuestByGuestIDFront(string guest_id)
        {
            DataTable dt = BAL.getGuestByGuestID(guest_id);

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }

            string json = js.Serialize(rows);
            return json;
        }


成功的javascript代码:

 success: function (serverdata) {

    var tableData = $.parseJSON(serverdata.d);
  }

关于c# - 如何在ASP.NET WebForm中的jQuery Ajax中使用C#数据表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21015429/

10-12 17:41