0

大家好,我正在努力将多个数组发布到 .net Web 服务,这是我的 Web 服务的签名。

<WebMethod()> _
Public Function Lib_Processor(ByVal w_vendor As String(), _
                             ByVal w_invoice As String(), _
                             ByVal w_invdate As String(), _
                             ByVal w_amount As Decimal(), _
                             ByVal w_account As String(), _
                             ByVal w_fund As String(), _
                             ByVal w_org As String(), _
                             ByVal w_prog As String(), _
                             ByVal w_adrsstyp As String(), _
                             ByVal w_adrss_seq As String(), _
                             ByVal w_Row As String(), _
                             ByVal w_bank As String(), _
                             ByVal w_user As String(), _
                             ByVal w_addl_info As String()) As List(Of GetErrors)

我正在循环一个表并将所有值放入对象中,然后使用 json.stringfy 正确创建要发送到服务的对象。

像这样。

        var invoice     = JSON.stringify({ w_invoice:w_invoice });
        var vendor      = JSON.stringify({ w_vendor: w_vendor });
        var invdate     = JSON.stringify({ w_invdate:w_invdate });
        var amount      = JSON.stringify({ w_amount:w_amount });
        var fund        = JSON.stringify({ w_fund:w_fund });
        var org         = JSON.stringify({ w_org:w_org });
        var prog        = JSON.stringify({ w_prog: w_prog });
        var account     = JSON.stringify({ w_account: w_account });
        var adrsstyp    = JSON.stringify({ w_adrsstyp:w_adrsstyp });
        var adrss_seq   = JSON.stringify({ w_adrss_seq:w_adrss_seq });
        var Row         = JSON.stringify({ w_Row:w_Row });
        var bank        = JSON.stringify({ w_bank:w_bank });
        var user        = JSON.stringify({ w_user:w_user });
        var addl_info   = JSON.stringify({ w_addl_info: w_addl_info });

然后我打电话给我的服务以发送数组。

$.ajax({

            type: "POST",

            contentType: "application/json; charset=utf-8",

            data: '{w_vendor:"' + w_vendor + '",w_invoice:"' + w_invoice + '",w_invdate:"'+w_invdate + "}",
            //data: "{"+vendor, invoice, invdate, amount,account,fund,org,prog,adrsstyp,adrss_seq,Row,bank,user,addl_info +"}",
           // "{"+ vendor+invoice+invdate+amount+account+fund+org+prog+adrsstyp+adrss_seq+Row+bank+user+addl_info+"}"
            url: "ServiceApUtils.asmx/Lib_Processor",

            dataType: "json",

            success: respond,

            error: function (e) { $('.result').html("An Error Occured"); }



        });

然而,这惨遭失败,我可以理解为什么,因为我只是试图将许多 json 对象发送到 1 服务。我怎样才能解决这个问题?我试过只做一个对象

 var thisInvoice = {INVOICE:INVOICE}

基本上

       var dto = { 'INVOICE': INVOICE };
       var PnJ = JSON.stringify(dto);

但这不起作用。如何正确调整此对象以发送到我的服务中?任何帮助将不胜感激。

4

2 回答 2

0

确保两件事:

  • 您的 json 字符串有效

  • Json 字符串名称与 pagemethod 上的属性名称匹配

此外,我建议您通过下面的链接来解释如何将复杂类型传递给 WebMethod。

http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

于 2013-07-12T17:54:52.007 回答
0

经过数小时的挫折后,我决定以自己的方式做事,这就是我想出的。我有一个我从中读取的 html 表,我需要使用 ajax 将 html 数据发送到 web 服务。我使用 jquery 读取表数据,如下所示。

/* webservice variables*/
var w_vendor = [];
var w_invoice = [];
var w_invdate = [];
var w_amount = [];
var w_account = [];
var w_fund = [];
var w_org = [];
var w_prog = [];
var w_adrsstyp = [];
var w_adrss_seq = [];
var w_Row = [];
var w_bank = [];
var w_user = [];
var w_addl_info = [];
var w_activity = [];
var w_location = [];
var w_bank = [];

然后在提交时我们简单地调用这些函数。

function GetAllTableRows() {
    try{

        //var MyInvoice = [];
        $('#ADPControlProcessor_GridView1 tbody tr').each(function (index, value) {
            var row = GetRow(index)
            //MyInvoice.push(row);


        });

        $.ajax({

            type: "POST",

            contentType: "application/json; charset=utf-8",

            data: '{' + 'w_vendor:[' + w_vendor + "],w_invoice:[" + w_invoice + "],w_invdate:[" + w_invdate + "]," + "w_amount:[" + w_amount + "],"+
                       "w_account:[" + w_account + "]," + "w_fund:[" + w_fund + "]," + "w_org:[" + w_org + "]," + "w_prog:[" + w_prog + "]," + "w_adrsstyp:[" + w_adrsstyp + "]," +
                       "w_adrss_seq:[" + w_adrss_seq + "]," + "w_Row:[" + w_Row + "]," + "w_bank:[" + w_bank + "]," + "w_user:[" + w_user + "]," + "w_addl_info:[" + w_addl_info+"]" + "}",

            url: "ServiceApUtils.asmx/Lib_Processor",

            dataType: "json",

            success: respond,

            error: function (e) { $('.result').html("An Error Occured"); }



        });




    }
    catch (err)
    {
        alert(err)

    }
    //return MyInvoice;
}
function respond() {

    alert('worked!')

}
function GetRow(rowNum)
{
    try{
        var row = $('#ADPControlProcessor_GridView1 tbody tr').eq(rowNum);


        w_vendor.push('"'+ row.find('td:eq(2)').text().trim()+'"');



        w_invoice.push('"' + row.find('td:eq(1)').text().trim()+'"'   );




        w_invdate.push('"' + row.find('td:eq(3)').text().trim() + '"');


        w_amount.push('"' + row.find('td:eq(4)').text().trim() + '"');

           w_fund.push('"' + row.find('td:eq(5)').text().trim() + '"');


           w_org.push('"' + row.find('td:eq(6)').text().trim() + '"');


           w_account.push('"' + row.find('td:eq(7)').text().trim() + '"');


           w_prog.push('"' + row.find('td:eq(8)').text().trim() + '"');


              w_activity.push('"' + row.find('td:eq(8)').text().trim() + '"');

              w_location.push('"' + row.find('td:eq(8)').text().trim() + '"');


              w_addl_info.push('"' + row.find('td:eq(11)').text().trim() + '"');



    w_adrsstyp.push('"' + row.find('td:eq(12)').text().trim() + '"');


    w_adrss_seq.push('"' + row.find('td:eq(13)').text().trim() + '"');


    w_Row.push('"' + row.find('td:eq(14)').text().trim() + '"');


    w_user.push('"' + "MIGUEL83_BANNER" + '"');

    w_bank.push('"' + "2" + '"');


    }
    catch (err)
    {
        alert (err)
    }

    //return INVOICE;
}

函数“GetAllTableRows()”只是调用 rows 函数来获取每一行并将值放入变量中。这是我在这个网站上实际找到的代码。但是我尝试使用 json stringify 但如果您查看我的服务的签名该服务需要 12 或 13 个字符串数组但是我看到的所有示例都希望您更改您的服务和创建一个类等等。好吧,我在这里没有那种能力,所以这是下一个最好的事情。你会怎么做?您创建自己的字符串,这就是我在“getRow()”函数上所做的,我用双引号将所有字符串连接起来,瞧。然后我根据 jquery 规范和 json 规范格式化了数据参数上的字符串。是的有点痛苦,但非常有用。事后看来,我本可以创建一个辅助函数来为我格式化所有字符串,但我会把它留给我未来的项目。希望这对其他人有帮助

于 2013-07-12T21:07:08.407 回答