2
是否可以读取从MVC控制器传递给JavaScript的数组内容?将数组从MVC控制器传递给Javascript
这是我的控制器返回一个数组的方法。 (之前尝试过但没有成功)
public string[] GetAllEvents()
{
string[] array = new string[2];
array[0] = "a";
array[1] = "b";
List<string> lst = new List<string>();
lst.Add("a");
lst.Add("b");
return array;
}
这是我从中调用Controller方法的JavaScript函数。
function GetAllEvents() {
$.ajax({
type: "GET",
url: "/Service/GetAllEvents",
success: function (result) {
alert(result.toString() + " " + result[0]);
},
error: function (req, status, error) {
//alert("Error");
}
});
};
结果是一个System.String [],结果[0]给我'S'作为结果。
您看到的结果可能实际上是字符串“System.String []”,而'result [0]'是该字符串中索引为0的字符,因此为“S”。按照[SLaks的答案](http://stackoverflow.com/a/10213690/74757),你应该看到预期的结果。 – 2012-04-18 16:36:52