如何将2个数组发送到JavaScript并在JavaScript文件的另一部分中使用它们? (对不起,我是一个新手,整个晚上都在努力解决这个问题)
我有一个包含2个数组的php文件。在JavaScript中,我有一个获取数组的ajax请求。但我只能回一个。我无法保存输出,因此我可以for each
以后再输出。
//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);
?>
答案 0 :(得分:3)
您只能将一个 json编码对象发送到javascript。要解决这个问题,请将两个数组放入一个数组中,然后对其进行编码并将其发回:
$return = ["array1"=>$array1, "array2"=>$array2];
echo json_encode($return);
您可以在那里的ajax电话中访问它:
//..
success: function(data) {
var array1 = data.array1;
},
//...