这是我的AJAX调用响应,它采用数组格式
[1,2,3,4,5,6]
success: function(outputfromserver) {
$.each(outputfromserver, function(index, el)
{
});
Run Code Online (Sandbox Code Playgroud)
我们怎样才能访问outputfromserver的所有值?
表示outputfromserver Zeroth值为1,第二个元素为2,-----依此类推
Wil*_*ein 11
了解您的AJAX请求是什么样子会有所帮助.我建议使用$ .ajax()并将dataType指定为JSON,或使用$ .getJSON().
下面是一个演示$ .ajax()的示例,并向您展示如何访问数组中返回的值.
$.ajax({
url: 'test.json', // returns "[1,2,3,4,5,6]"
dataType: 'json', // jQuery will parse the response as JSON
success: function (outputfromserver) {
// outputfromserver is an array in this case
// just access it like one
alert(outputfromserver[0]); // alert the 0th value
// let's iterate through the returned values
// for loops are good for that, $.each() is fine too
// but unnecessary here
for (var i = 0; i < outputfromserver.length; i++) {
// outputfromserver[i] can be used to get each value
}
}
});
Run Code Online (Sandbox Code Playgroud)
现在,如果您坚持使用$ .each,则以下内容适用于成功选项.
success: function (outputfromserver) {
$.each(outputfromserver, function(index, el) {
// index is your 0-based array index
// el is your value
// for example
alert("element at " + index + ": " + el); // will alert each value
});
}
Run Code Online (Sandbox Code Playgroud)
随意问任何问题!
| 归档时间: |
|
| 查看次数: |
80858 次 |
| 最近记录: |