问题
in something like :
$.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"); }
});
how do I send more than one array to a Web Service? Is it possible? Or should i send multi-dimensional array?
回答1:
Assuming your the web service you're talking to is ready for that, I suggest going to multi-array route. An array in json would look like:
[1,2,3]
So, you could pass multiple arrays in an array like this:
[[1,2,3],[4,5,6]]
回答2:
I got it: in my javascript I have:
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");
}
});
}
then in my web service (asp.net web service):
[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();
}
来源:https://stackoverflow.com/questions/1712009/jquery-and-web-services-can-i-pass-more-than-one-array-in-data-attribute