不知道为什么我的AJAX调用不会在webforms中填充数据

时间:2017-07-24 21:23:42

标签: javascript c# jquery ajax webforms

当页面准备好后,我希望它调用将在GridView中填充数据的函数BindGridView。我不知道为什么它不会进入Ajax的“成功”部分。我在另一个ajax调用中做了类似的事情,它运行正常。我完全按照教程。我得到500(内部服务器错误)。

protected void Page_Load(object sender, EventArgs e)
        {
            BindDummyItem();
        }

        public void BindDummyItem()
        {
            DataTable dtGetData = new DataTable();
            dtGetData.Columns.Add("ID");
            dtGetData.Columns.Add("ServerName");;
            dtGetData.Columns.Add("Command");
            dtGetData.Columns.Add("CreateDate");
            dtGetData.Rows.Add();

            GridView1.DataSource = dtGetData;
            GridView1.DataBind();
        }

[WebMethod]
        public static sObj[] test()
        {
            string cs = ConfigurationManager.ConnectionStrings["asdasd"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            DataTable dataTable = new DataTable();
            List<s> sl = new List<s>();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("stredP", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add(new SqlParameter("@StarteDate", "2017-07-24 19:20:00"));
                da.SelectCommand.Parameters.Add(new SqlParameter("@EndDate", "2017-07-24 19:21:00"));

                da.Fill(dataTable);

                foreach (DataRow dtRow in dataTable.Rows)
                {
                    sObj DataObj = new sObj();
                    DataObj.ID = Convert.ToInt64(dtRow["ID"]);
                    DataObj.ServerName = dtRow["ServerName"].ToString();
                    DataObj.Command = dtRow["Command"].ToString();
                    DataObj.CreateDate = Convert.ToDateTime(dtRow["CreateDate"]);
                    sl.Add(DataObj);
                }
            }
            catch (Exception ex)
            {
                //TODO
            }
            finally
            {
                con.Close();
            }
            return sl.ToArray();
        }

.aspx中的代码

<script>
    $(document).ready(function () {
        BindGridView();
    });

    function BindGridView() {
        $.ajax({
            type: "POST",
            url: "ProjectName.aspx/test",
            contentType: "application/json;charset=utf-8",
            data: {},
            dataType: "json",
            success: function (data) {
                console.log("dfgdgdfgdfg");
                $("#GridView1").empty();

                if (data.d.length > 0) {

                    for (var i = 0; i < data.d.length; i++) {

                        $("#GridView1").append("<tr><td>" + 
                        data.d[i].ID + "</td> <td>" +
                        data.d[i].ServerName + "</td> <td>" +
                        data.d[i].Command + "</td></tr>" +
                        data.d[i].CreateDate + "</td></tr>");
                    }
                }
            },
            error: function (result) {
                //alert("Error login");

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

在Web.Config中设置此项将增加长度:

<system.web.extensions>
    <scripting>
      <webServices>
          <jsonSerialization maxJsonLength="2147483647"/>
      </webServices>
    </scripting>
</system.web.extensions>

基于此:

https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength(v=vs.110).aspx