来自webmethod在javascript中返回二维数组

时间:2014-04-19 15:03:21

标签: javascript asp.net webmethod

我有点问题。从webmethod返回二维数组。

我的网络方法:

[WebMethod]
public static string[,] Test()
{
string[,] arry1 = new string[2, 2];
arry1[0, 0] = "test00";
arry1[0, 1] = "test01";
arry1[1, 0] = "test10";
arry1[1, 1] = "test11";

return arry1; 
}

在我的js代码中使用这种方式....

var arry1=new Array();
$.ajax({
url: "test.aspx/Test",
data: {},
cache: false,
async:false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
success: function (data) {
    arry1 = data.d;



},
error: function (response) {
    alert(response);
}
});
alert(arry1[1,1]); //test01  why not test11?

我该怎么做?

编辑.. 阵列

"test00" "test01";
"test10" "test11";

在asp.net中

arry1[0, 0] = "test00";
arry1[0, 1] = "test01";
arry1[1, 0] = "test10";
arry1[1, 1] = "test11";

在javascript中

arry1[0] // test00
arry1[1] // test01
arry1[2] // test10
arry1[3] // test11

2 个答案:

答案 0 :(得分:1)

根据您运行alert(arry1[1,1]);的数组,只返回位置1的值,如果是JavaScript,则返回数组,因此如果您愿意,结果将为,test01获得test11,您必须执行alert(arry1[3][1]);,这将获得数组[3]中的第四个位置,然后获取该数组[1]处的值。

//What your array might like in JS
var arry1 = [[null,"test00"], [null,"test01"], [null,"test10"], [null,"test11"]];
alert(arry1[1,1]); // test01
alert(arry1[3][1]); // test11

Fiddle

答案 1 :(得分:0)

与其他语言不同,Javascript确实有2D数组。相反,它有阵列数组。

这意味着您需要稍微不同的语法

alert(arry1[1][1]);