使用WebMethod和Jquery Ajax获取内部服务器错误时无法在gridview中显示数据?

时间:2015-09-08 10:29:28

标签: javascript c# jquery asp.net ajax

我正在尝试使用Jquery和Ajax中的WebMethod在Gridview中显示数据。我在SQL Server数据库中使用存储过程进行分页并返回每页数据。当在浏览器上呈现页面时没有显示数据但是jquery的alert()函数被触发,表明我收到错误。在单击inspect元素时,我看到错误是 - “无法加载资源:服务器响应状态为500(内部服务器错误)”

Aspx Page

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
    body
    {
        font-family: Arial;
        font-size: 10pt;
    }
    .Pager span
    {
        text-align: center;
        color: #999;
        display: inline-block;
        width: 20px;
        background-color: #A1DCF2;
        margin-right: 3px;
        line-height: 150%;
        border: 1px solid #3AC0F2;
    }
    .Pager a
    {
        text-align: center;
        display: inline-block;
        width: 20px;
        background-color: #3AC0F2;
        color: #fff;
        border: 1px solid #3AC0F2;
        margin-right: 3px;
        line-height: 150%;
        text-decoration: none;
    }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/ASPSnippets_Pager.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        GetABC(1);
    });
    $(".Pager .page").live("click", function () {
        GetABC(parseInt($(this).attr('page')));
    });
    function GetABC(pageIndex) {
        $.ajax({
            type: "POST",
            url: "ABC.aspx/GetABC",
            data: '{pageIndex: ' + pageIndex + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert("Failure");
            },
            error: function (response) {
                alert("Error");
            }
        });
    }

    function OnSuccess(response) {

        var xmlDoc = $.parseXML(response.d);
        var xml = $(xmlDoc);
        var xyz = xml.find("table1");
        var row = $("[id*=GridView1] tr:last-child").clone(true);
        $("[id*=GridView1] tr").not($("[id*=GridView1] tr:first-child")).remove();
        $.each(xyz, function () {
            var abc= $(this);
            $("td", row).eq(0).html($(this).find("XXX").text());
            $("td", row).eq(1).html($(this).find("XXX").text());
            $("td", row).eq(2).html($(this).find("XXX").text());
            $("td", row).eq(3).html($(this).find("XXX").text());
            $("td", row).eq(4).html($(this).find("XXX").text());
            $("td", row).eq(5).html($(this).find("XXX").text());
            $("td", row).eq(6).html($(this).find("XXX").text());
            $("td", row).eq(7).html($(this).find("XXX").text());
            $("[id*=GridView1]").append(row);
            row = $("[id*=GridView1] tr:last-child").clone(true);
        });
        var pager = xml.find("Pager");
        $(".Pager").ASPSnippets_Pager({
            ActiveCssClass: "current",
            PagerCssClass: "pager",
            PageIndex: parseInt(pager.find("PageIndex").text()),
            PageSize: parseInt(pager.find("PageSize").text()),
            RecordCount: parseInt(pager.find("RecordCount").text())
        });
    };
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3AC0F2" 
        HeaderStyle-ForeColor="White">
    <Columns>
    <asp:BoundField ItemStyle-Width="150px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="150px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="150px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="150px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="200px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="150px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="5px" HeaderText="XXX" DataField="XXX" />
    <asp:BoundField ItemStyle-Width="5px" HeaderText="XXX" DataField="XXX" />
    </Columns>
    </asp:GridView>
    <br />
    <div class="Pager"></div>
</asp:Content>

CS页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;
public partial class ABC: System.Web.UI.Page
{
    private static int PageSize = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Columns.Add("xxx");
        dt.Rows.Add();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    [WebMethod]
    public static string GetABC(int PageIndex)
    {
        string query = "[GetABC_Pager]";
        SqlCommand cmd = new SqlCommand(query);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PageIndex", PageIndex);
        cmd.Parameters.AddWithValue("@PageSize", PageSize);
        cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
        return GetData(cmd, PageIndex).GetXml();
    }
    private static DataSet GetData(SqlCommand cmd, int pageIndex)
    {
        string strConnstring = ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnstring))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds, "table1");
                    DataTable dt = new DataTable("Pager");
                    dt.Columns.Add("PageIndex");
                    dt.Columns.Add("PageSize");
                    dt.Columns.Add("RecordCount");
                    dt.Rows.Add();
                    dt.Rows[0]["PageIndex"] = pageIndex;
                    dt.Rows[0]["PageSize"] = PageSize;
                    dt.Rows[0]["RecordCount"] = cmd.Parameters["@RecordCount"].Value;
                    ds.Tables.Add(dt);
                    return ds;
                }
            }
        }
    }
}

页面加载警报(“错误”)函数被触发并且gridview中没有数据。当我在我的Localhost上运行时,相同的代码没有显示错误,但是当我将相同的文件传输到我的服务器时,我收到此错误。请指导我做错的地方,我还需要做其他事情。我已按照本文开发此应用程序 - http://www.aspsnippets.com/Articles/Retrieve-data-from-Database-and-display-on-page-using-jQuery-AJAX-in-ASPNet.aspx

0 个答案:

没有答案