在$ .ajax()函数中访问两个不同的数组

时间:2021-01-17 15:53:30

I've got these two pages. One echoes a JSON encoded array and the other uses the JQuery $.ajax syntax to access it. The code looks something like this:

我有这两页。一个回应JSON编码的数组,另一个使用JQuery $ .ajax语法来访问它。代码看起来像这样:

Page 1

$array = blah blah
echo json_encode($array);

Page 2

function load(){
  $.ajax({                                      
    url: 'json_array_file.php',                                                                   
    data: 'value=<?php echo $_GET["session"]; ?>',       
    dataType: 'json',                                                                       
    success: function(data) {                                                       
        var idCurrent = data[0];                                                    
        var idVideo = data[1];
        var idSession = data[2];
        var state = data[4];
        //do stuff with variables
    }
  });
}

So the trouble I'm having is that I now need to access two arrays from the first page but I'm not sure what the syntax is to access that second array within the $.ajax function on the second page. Anyone know how?

所以我遇到的麻烦是我现在需要从第一页访问两个数组,但我不确定在第二页的$ .ajax函数中访问第二个数组的语法是什么。谁知道怎么样?

2 个解决方案

#1


4  

On the PHP side:

在PHP方面:

<?php
   $arr1 = array(1, 2, 3, 4);
   $arr2 = array(5, 6, 7, 8);

   echo json_encode(array('arr1' => $arr1, 'arr2' => $arr2));
?>

On the jQuery side:

在jQuery方面:

/* ... stuff ... */
success: function(data) {
    var fromArray1 = data.arr1[0];
    var fromArray2 = data.arr2[0];
    /* etc. */
}
/* ... other stuff ... */

#2


0  

Maybe returning an multidimensional array helps.

也许返回一个多维数组有帮助。

#1


4  

On the PHP side:

在PHP方面:

<?php
   $arr1 = array(1, 2, 3, 4);
   $arr2 = array(5, 6, 7, 8);

   echo json_encode(array('arr1' => $arr1, 'arr2' => $arr2));
?>

On the jQuery side:

在jQuery方面:

/* ... stuff ... */
success: function(data) {
    var fromArray1 = data.arr1[0];
    var fromArray2 = data.arr2[0];
    /* etc. */
}
/* ... other stuff ... */

#2


0  

Maybe returning an multidimensional array helps.

也许返回一个多维数组有帮助。