通过Json将PHP数组转换为JavaScript

时间:2021-12-08 21:16:04

Hey, I've got a PHP file that that echo's an array that has been encoded with json_encode. This file is used by the jQuery ajax function to retrieve the array. I am unable to figure out how to use the array though, I have managed to alert variables but not arrays. Here is my code:

嘿,我有一个PHP文件,回声是一个用json_encode编码的数组。 jQuery ajax函数使用此文件来检索数组。虽然我无法弄清楚如何使用数组,但我设法提醒变量而不是数组。这是我的代码:

function sessionStatus(){
    $(document).ready(function(){
        $.ajax({type:"POST", url:scriptRoot+"sessionStatus.php", success:function(data){
                alert(data[0]);
            }
        });
    });
}

2 个解决方案

#1


1  

json_encode(array("data1","data2"));


$.getJSON("sessionStatus.php",function(data){
      alert(data[0]);
});

Should alert "data1"

应提醒“data1”

#2


0  

you need to use

你需要使用

data.property as an object not like an array[property]

data.property作为一个不像数组[property]的对象

#1


1  

json_encode(array("data1","data2"));


$.getJSON("sessionStatus.php",function(data){
      alert(data[0]);
});

Should alert "data1"

应提醒“data1”

#2


0  

you need to use

你需要使用

data.property as an object not like an array[property]

data.property作为一个不像数组[property]的对象