dss524049 2017-09-14 21:01
浏览 78

如何向JavaScript发送2个不同的PHP数组?

How can I send 2 arrays to JavaScript and use them in another part of the JavaScript file? (sorry I'm a newbie and struggling all evening with this question)

I have a php file with 2 arrays. In the JavaScript I have an ajax request to get the arrays. But I can return only one. And I cannot save the output so I can for each it later.

//Get arrays

 $.ajax(
 {
   type: 'get',
   url: '../classes/bestanden_overzicht_client.php',
   data: "id="+id,
     success:function(data)//we got the response of ajax :)
     {
    <-- how can i target the 2 arrays and send them to function below --> 
     },
     error:function(data)
     {
     //no respons show error.
     alert  ("error!");
     alert(JSON.stringify(data)) //we give an alert of the error when there is no respons from ajax
     ;}
});

("#images").fileinput({
        uploadAsync: false,
        overwriteInitial: false,
initialPreview: [

<-- want here the output of the array1


],
intialConfig [

<-- Want here the output of array2

]



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
$array1 = array();
$array2 = array();

$array1[0] = "numbers"; 
$array1[1] = "letters"; 

$array2[0] = "forname"; 
$array2[0] = "surname"; 

echo json_encode($array1);
echo json_encode($array2);
?>
  • 写回答

1条回答 默认 最新

  • douluan8828 2017-09-14 21:21
    关注

    You can only send one json encoded object to javascript. To solve that, put your two arrays into one array, then encode that and send it back:

    $return = ["array1"=>$array1, "array2"=>$array2]; 
    echo json_encode($return);
    

    you'll be able to access it in your ajax call there:

    //..
    success: function(data) {
        var array1 = data.array1;
    },
    //...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探