[英]how to get two or more array data from php page to javascript using json
The code below is in EAddEmployeeAddAndOtherParts.php
page: 下面的代码在
EAddEmployeeAddAndOtherParts.php
页面中:
$array1 = array("i am first array");
$array2 = array("i am second array");
echo json_encode($array1,$array2);
The code below is in the home.html
page: 下面的代码在
home.html
页面中:
$(document).ready( function() {
$.ajax({
type: 'POST',
url: 'EAddEmployeeAddAndOtherParts.php',
data: 'aa='+aa+'&f='+f,
dataType: 'html',
cache: false,
success: function(result1,result2) {
alert(result1[0]);
alert(result2[0]);
},
});
});
My question is: How can I get $array1
and $array2
data in my home.html
page using JSON? 我的问题是:如何使用JSON在
home.html
页面中获取$array1
和$array2
数据? My code above is not working. 我上面的代码无法正常工作。
The json_encode()
only uses the first argument as the data, so wrap your two arrays into a single array: json_encode()
仅将第一个参数用作数据,因此将您的两个数组包装为一个数组:
echo json_encode(array('array1' => $array1, 'array2' => $array2));
In the ajax call, the response is the first argument, and because in the above, we used keys, we can directly reference result.array1
. 在ajax调用中,响应是第一个参数,由于在上面我们使用了键,因此可以直接引用
result.array1
。 So change to 所以改为
dataType: 'json',
success: function(result) {
alert(result.array1);
alert(result.array2);
},
Also note, I changed the dataType to json
. 还要注意,我将dataType更改为
json
。 If you continue to use html
as the dataType, then jQuery won't automatically parse the JSON for you. 如果您继续使用
html
作为dataType,那么jQuery不会自动为您解析JSON。
header('Content-type:application/json');
echo json_encode(array($array1,$array2));
exit;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.