使用JavaScript将多个数组发布到.net Web服务

时间:2013-07-12 17:42:21

标签: javascript json jquery

大家好我正在努力将多个阵列发布到.net网络服务这里是我的网络服务的签名。

<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);

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

2 个答案:

答案 0 :(得分:0)

确保两件事:

  • 您的json字符串有效

  • Json字符串名称与pagemethod

  • 上的属性名称匹配

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

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

答案 1 :(得分: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函数来获取everysingle行并将值放入变量中。这是我在这个网站上实际找到的代码。 然而,我试图使用json stringify,但它只是不能满足我对这个特殊情况的需求,如果你看看我的服务的签名服务期望12或13个字符串数组但是我见过的所有例子都希望你改变你的服务和创建一个类等等。嗯,我没有这种能力,所以这里是下一个最好的事情。你会怎么做?你创建自己的字符串,这就是我在“getRow()”函数上所做的,我用双引号连接所有字符串和瞧。然后我根据jquery规范和json规范在数据参数上格式化了字符串。是有点痛苦,但非常有用。事后我可以创建一个帮助函数来为我生成格式化所有字符串但是生病留给我未来的项目。希望这有助于其他人