我试图循环从 PHP
返回的数组。但是还是不明白。例子是~
PHP:
$items = array();
$items["country"] = "North Korea",
$items["fruits"] = array(
"apple"=>1.0,
"banana"=>1.2,
"cranberry"=>2.0,
);
echo json_encode( $fruits );
jQuery:
$.ajax({
url: "items.php",
async: false,
type: "POST",
dataType: "JSON",
data: { "command" : "getItems" }
}).success(function( response ) {
alert( response.fruits.apple ); //OK
// <------ here, how do i loop the response.fruits ? -----
});
请问我怎样才能循环知道我得到了哪些水果?
最佳答案
你可以这样做:
$.each(response.fruits,function(key,value){
console.log(key+":"+value);
});
关于javascript - 如何在 jQuery Ajax Success 返回的数组中循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23733616/