使用jQuery ajax将数组数组发布到webmethod

时间:2018-03-23 16:05:28

标签: jquery json ajax vb.net

jQuery代码从gridvew行构建数组:

var a = 0;
$('[id*=GridView_PaymentSchedule]').find('tr:has(td)').each(function () {
    payments[a] = []
    payments[a] = [$(this).find('td').eq(0)[0].innerHTML, $(this).find('td').eq(1)[0].innerHTML, $(this).find('td').eq(2)[0].innerHTML, $(this).find('td').eq(3)[0].innerHTML];
    a += 1;
});

我的ajax电话:

$.ajax({
    type: "POST",
    url: "PaymentPlan.aspx/SaveGridViewData",
    data: '{pPayments: ' + JSON.stringify({ payments: payments }) + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    success: function (response) {
    alert("Data sucessfully transmited.");
        window.location.reload();
    },
    error: function (response) {
        alert("Data not transmited: error = " + response.responseText);
    }  
});

webmethod成功执行,但我得到的是一个空字符串列表,webmethod代码是:

<WebMethod()> _
Public Shared Sub SaveGridViewData(ByVal pPayments As List(Of string))
    Dim x As Integer
    x += 1
End Sub

我保持简单,我在x += 1有一个断点,所以我可以检查pPayments参数。

那么我错过了什么?

1 个答案:

答案 0 :(得分:0)

好的,我明白了。原来是客户端的一行附加代码。我改变了构建JSON对象的方式,而不是构造对象数组的数组数组:

jQuery(ajax)代码:

var thepayment = new Array();

$('[id*=GridView_PaymentSchedule]').find('tr:has(td)').each(function () {
    var obj = new Object;
    obj.paymentdate = $(this).find('td').eq(0)[0].innerHTML;
    obj.amount = $(this).find('td').eq(1)[0].innerHTML;
    obj.processingfee = $(this).find('td').eq(2)[0].innerHTML;
    obj.totalamount = $(this).find('td').eq(3)[0].innerHTML;

    thepayment.push(obj);
});
// This is the line of code that made the difference
// DTO stands for Data Transfer Object
// Then you JSON stringify it, not all at once.

// This DOES NOT work:
// '{pPayments: ' + JSON.stringify({ payments: thepayment }) + '}'

// This does:
var DTO = { 'pPayments': thepayment };

$.ajax({
    type: "POST",
    url: "PaymentPlan.aspx/SaveGridViewData",
    data: JSON.stringify(DTO), // '{pPayments: ' + JSON.stringify({ payments: thepayment }) + '}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    traditional: true,
    success: function (response) {
        alert("Data sucessfully transmited.");
        window.location.reload();
    },
    error: function (response) {
        alert("Data not transmited: error = " + response.responseText);
    }

});

付款类:

Public Class payments
    Public Sub New()
        MyClass.New("", "", "", "")
    End Sub
    Public Sub New(ByVal pPaymentdate As String, ByVal pAmount As String, ByVal pProcessingfee As String, ByVal pTotalamount As String)
        paymendate = pPaymentdate
        amount = pAmount
        processingfee = pProcessingfee
        totalamount = pTotalamount
    End Sub

    Public Property paymendate As String

    Public Property amount As String

    Public Property processingfee As String

    Public Property totalamount As String

End Class

网络方法:

<WebMethod()> _
Public Shared Sub SaveGridViewData(ByVal pPayments As List(Of payments))
    ' Do Stuff
End Sub