Jquery和Web服务:我可以在数据属性中传递多个数组

时间:2009-11-10 23:34:04

标签: jquery web-services

之类的:

$.ajax({

   type: "POST",    
   url: "WebService1.asmx/SendValues",    
   data: jsonText,    
   contentType: "application/json; charset=utf-8",    
   dataType: "json",    
   success: function() { alert("it worked"); },    
   failure: function() { alert("Uh oh"); }

   });

如何向Web服务发送多个阵列?可能吗?或者我应该发送多维数组?

2 个答案:

答案 0 :(得分:1)

假设您正在与之交谈的Web服务已做好准备,我建议您采用多阵列路由。 json中的数组看起来像:

[1,2,3]

因此,您可以在数组中传递多个数组:

[[1,2,3],[4,5,6]]

答案 1 :(得分:1)

我明白了: 在我的javascript中我有:

function Save() {
     var list = [["1","2","3"],["4","5","6"],["34","88","898"]];
     var jsonText = JSON.stringify({ list: list });

     $.ajax({
         type: "POST",
         url: "http://localhost/TemplateWebService/TemplateWebService/Service.asmx/SaveSampleTemplate",
         data: jsonText,
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function(response) {
             //var arrSamples = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
         alert("success!");

             alert(response.d);


         },

         failure: function(msg) {

             alert("fail");                 

         }

     });

}

然后在我的网络服务(asp.net网络服务)中:

[WebMethod()]
        public string SaveSampleTemplate(string[][] list)
        {

            List<string[]> mylist = list.ToList();


           // return list.Count.ToString();
            string str = "";
            for (int i = 0; i < mylist.Count; i++)
            {
                string[] m = mylist[i];

                for (int j = 0; j < m.Length; j++)
                {
                    str += m[j] + " ";
                }

            }
            return str;

          // return list.Length.ToString();
        }