编辑--这是我从
$(document).ready(function()
{
$.ajax({
method: "get",
url: 'ctr_seearmylist.php',
dataType: 'jsonp',
data: 'get=squad',
success: processSquads
});
});这是创建响应的php代码段:
{..... //iterates throuh a result taken from the database
$temp[0]=$id;
$temp[1]=$squad_id;
$result[]=$temp;
}
$result=json_encode($result);
}
return $result;
}如果我呼叫警报(response.constructor);我得到
function Array() {
[native code]
}端编辑
如何使用jquery或javascript或其他任何方法迭代json数组?
我得到的json答复有如下形式:["1“、"12”、"2“、"3”、"3“、"7"]
我应该指出,使用response.length;没有任何效果。
function processSquads(response)
{
alert (response[0][0]); // works and returns 1
alert (response[0]); // works and returns 1,12
alert (response.length); //doesn't work so I can't iterate
}抱歉,今天的问题太多了,但是我刚刚开始使用Ajax,我陷入了困境。
发布于 2011-01-16 20:35:41
使用Jquery:
var arr = [["1","12"],["2","3"],["3","7"]];
jQuery.each(arr, function() {
alert(this[0] + " : " + this[1]);
});
//alerts: 1 : 12, etc.这将迭代数组,然后显示索引0和1中的内容。
发布于 2011-01-16 20:33:10
那不是一个json数组,而是一个数组
这应该很好:http://jsfiddle.net/w6HUV/2/
var array = [["1", "12"], ["2", "3"], ["3", "7"]];
processSquads(array);
function processSquads(response) {
alert(response[0][0]); // 1
alert(response[0]); // 1, 12
alert(response.length); // 3
$(array).each(function(i){
alert(response[i]); // 1,12 - 2,3 - 3,7
});
}发布于 2011-01-16 20:37:28
未经测试,但这应该有效:
function processSquads(response)
{
for(var list in response)
{
for(var item in response)
{
alert(item);
}
}
}https://stackoverflow.com/questions/4707851
复制相似问题