【发布时间】:2015-07-01 14:45:39
【问题描述】:
我有以下代码。每当我更改数组中的值时 - data[0] 说data[1] 值都会更改。我在data 数组中存储了大约 4 个项目。
$(document).ready(function() {
$.ajax({
cache: false,
url: "http://<mywebsite>/user/id/1",
type: 'GET',
crossDomain: true,
dataType: 'json',
success: function() {
alert("Success");
},
error: function() {
alert('Failed!');
},
}).then(function(data) {
var result = data [1];
console.log(result);
$('.ch-name').append(result.ch_name);
$('.ch-logo').append(result.ch_logo);
$('.ch-desc').append(result.ch_desc);
$('.ch-genre').append(result.ch_genre);
});
});
我想显示数组中的所有数据。我怎么做?我试过这样做,但没有奏效。我也尝试过其他方法,但仍然如此。
$(document).ready(function() {
$.ajax({
cache: false,
url: "http://<mywebsite>/user/id/1",
type: 'GET',
crossDomain: true,
dataType: 'json',
success: function() {
alert("Success");
},
error: function() {
alert('Failed!');
},
}).then(function(data) {
var result = data [1];
console.log(result);
for (i = 0; i < result.length; i++) {
$('.ch-name').append(result[i].ch_name);
$('.ch-logo').append(result[i].ch_logo);
$('.ch-desc').append(result[i].ch_desc);
$('.ch-genre').append(result[i].ch_genre);
}
});
});
【问题讨论】:
-
你收到的
data是什么格式的? -
“没用”是什么意思?
-
你的
console.log(result);是什么样的? -
这取决于结果数组的内容,您可以发布console.log输出吗?
-
@Huey 这是一个数组。数据类型为json。
标签: javascript jquery arrays loops