Jquery Ajax Call没有成功

时间:2013-03-07 21:40:26

标签: jquery ajax vb.net call webmethod

我有一个对webmethod的ajax调用但是调用总是失败。 这是下面需要的代码。 LetterReportDataStore类进入数据库并填充并返回数据表。

    $(document).ready(function () {
           BindGridView('eastern');

    });

    function BindGridView(r) {
        $.ajax({
            type: "POST",
            url: "AgencyLetterReport.aspx/GetLetterReportData",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert('Success');
                for (var i = 0; i < data.d.length; i++) {
                    $("#grdviewe").append("<tr><td>" + data.d[i].Username +
                                          "</td><td>" + data.d[i].Name + "</td></tr>");
                }
                                },
            error: function () { alert('Failed'); }
        })
    }

    <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetLetterReportData() As LetterReportData()
    Dim region As String = "eastern"
    Dim list As List(Of LetterReportData) = New List(Of LetterReportData)
    Dim dt As DataTable = LetterReportDataStore.LoadGridData(region)

    For Each r As DataRow In dt.Rows
        Dim _data As LetterReportData = New LetterReportData
        _data.Username = r.Item(0)
        _data.Name = r.Item(1)

        list.Add(_data)
    Next
    Return list.ToArray()
End Function

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。我改变了我的webmethod到下面。 我在其中一个数据字段中获取了一些空值,因此我使用.Tostring将其设为空字符串。我还将List更改为ArrayList,但我认为List也可以正常工作。问题是我相信的空值。

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetLetterReportData(ByVal region As String) As ArrayList
    'Dim region As String = "eastern"
    Dim list As ArrayList = New ArrayList
    Dim dt As DataTable = LetterReportDataStore.LoadGridData(region)

    For Each r As DataRow In dt.Rows
        Dim _data As LetterReportData = New LetterReportData
        _data.Username = r.Item(0).ToString
        _data.Name = r.Item(1).ToString

        list.Add(_data)
    Next
    Return list
    'Return region
End Function

这是我的jquery调用

$(document).ready(function () {
        $("#tabs").tabs();
        BindGridView('eastern');          
    });

    function BindGridView(r) {
        $.ajax({
            type: "POST",
            url: "AgencyLetterReport.aspx/GetLetterReportData",
            data: "{'region' : '" + r + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                //alert(data.d[0].Username);
                for (var i = 0; i < data.d.length; i++) {
                    $("#grdviewe").append("<tr class='GridviewScrollC1Item'><td>" + data.d[i].Username +
                                          "</td><td>" + data.d[i].Name + "</td>" +
                                          "</tr>");
                }
                $('#grdviewe').gridviewScroll({
                    width: $(window).width() - 200,
                    height: $(window).height() - 200
                });
            },
            error: function () { alert('Error on loading data.'); }
        })
    }

如果你在.append调用中注意到这一行,则class ='GridviewScrollC1Item'。我不得不添加这个,因为gridview列在数据绑定上丢失了css并使列不与标题对齐。 虽然gridview设置如下。标题css保持不变,但是alternaterow样式和rowstyle丢失了。我正在努力尝试修复改变的行样式。

 <asp:GridView ID="grdviewe" runat="server" Width="100%" ShowHeaderWhenEmpty="True">
     <AlternatingRowStyle BackColor="#EEF2FF" />
     <HeaderStyle CssClass="GridviewScrollC1Header" /> 
     <RowStyle CssClass="GridviewScrollC1Item" />
 </asp:GridView>

希望这对任何人都有帮助!