[英]PHP json_encode to JS
PHP json_encode as AJAX var "result": PHP json_encode作为AJAX变量“结果”:
[{"id":"139","assettypeid":"3","name":"skin1","body":"skin1.jpg"}]
I'm trying to access each property, but I can't: 我正在尝试访问每个属性,但是我不能:
for (var i =0;i < result.length-1;i++)
{
var item = result[i];
console.log (item.id + item.name + item.body);
}
All I see is: 我所看到的是:
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
...
And there are way too many iterations ... as you can see in the JSON above, there should only be 4 loops. 迭代的方式太多了……正如您在上面的JSON中看到的那样,应该只有4个循环。
you need to use JSON.parse 您需要使用JSON.parse
var items = JSON.parse(result)
UPDATE: 更新:
I modified my answer to create a JSON call to a server side file like PHP or Ruby. 我修改了答案,以创建对服务器端文件(如PHP或Ruby)的JSON调用。 If you are using jQuery try this instead: 如果您使用的是jQuery,请尝试以下方法:
$.ajax({
url: 'http://url-of-your-server-side.com/server-side-file-name.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
success: function(data) {
$.each(data, function(i,item){
console.log (item.id + item.name + item.body);
});
error: function(){
// execute upon failure
}
Data is the variable that holds your array provided by your ajax request. 数据是保存由ajax请求提供的数组的变量。
for(var item in result){
console.log(item.id, item.name, item.body);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.