如何使用 jQuery AJAX 和 PHP 数组返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15311320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to work with jQuery AJAX and PHP array return
提问by blasteralfred Ψ
I have a jquery ajax request like;
我有一个 jquery ajax 请求,例如;
$.ajax({
type: 'POST',
url: 'processor.php',
data: 'data1=testdata1&data2=testdata2&data3=testdata3',
cache: false,
success: function(result) {
if(result){
alert(result);
}else{
alert("error");
}
}
});
The handler processor.phpis set to return an array like;
处理程序processor.php设置为返回一个数组,如;
$array = array("a","b","c","d");
echo $array;
I want to do action in client side based on this. Say if array[0] is 'b', I want to alert "hi". Again if array[2] is 'x', I want to alert "hello", and so on. How can I filter array elements in order to grab their data?
我想基于此在客户端执行操作。假设 array[0] 是 'b',我想提醒“hi”。同样,如果 array[2] 是 'x',我想提醒“你好”,依此类推。如何过滤数组元素以获取其数据?
回答by Abu Roma?ssae
You will have to return the array encoded in the json form like following
您必须返回以 json 形式编码的数组,如下所示
$array = array("a","b","c","d");
echo json_encode($array);
then you can access it in javascript converting it back to an array/object like
然后你可以在javascript中访问它,将它转换回一个数组/对象,比如
var result = eval(retuned_value);
You can also navigate through all array elements using a for loop
您还可以使用 for 循环浏览所有数组元素
for (var index in result){
// you can show both index and value to know how the array is indexed in javascript (but it should be the same way it was in the php script)
alert("index:" + index + "\n value" + result[index]);
}
in your code it should look something like:
在您的代码中,它应该类似于:
PHP Code:
PHP代码:
$array = array("a","b","c","d");
echo json_encode( $array );
jQuery script
jQuery 脚本
$.ajax({
type: 'POST',
url: 'processor.php',
data: 'data1=testdata1&data2=testdata2&data3=testdata3',
cache: false,
success: function(result) {
if(result){
resultObj = eval (result);
alert( resultObj );
}else{
alert("error");
}
}
});
回答by HMR
Return JSON from php http://php.net/manual/en/function.json-encode.php
从 php http://php.net/manual/en/function.json-encode.php返回 JSON
and in javascript create an object from the json string, you can do this with using getJSON instead of ajax http://api.jquery.com/jQuery.getJSON/
并在 javascript 中从 json 字符串创建一个对象,您可以使用 getJSON 而不是 ajax http://api.jquery.com/jQuery.getJSON/
Make sure your php sets the right response header:
确保您的 php 设置了正确的响应标头:
header ("content-type: application/json; charset=utf-8");
回答by ChrisH
I find the best way to return an array from php to Ajax (jscript):
我找到了将数组从 php 返回到 Ajax (jscript) 的最佳方法:
on the php side: echo json_encode($myArray);on the javascript side (e.g. myAjax.responseText)
replyVal = JSON.parse(myAjax.responseText);
在 php 方面:echo json_encode($myArray);在 javascript 方面(例如myAjax.responseText)
replyVal = JSON.parse(myAjax.responseText);
To send arrays TO php from javascript, use the matching JSON.stringify()to send
and php json_decode()to receive
要将数组从 javascript 发送到 php,请使用匹配JSON.stringify()发送和 phpjson_decode()接收
回答by aphoe
In your PHP code encode the array as JSON object
在您的 PHP 代码中,将数组编码为 JSON 对象
echo json_encode($array);
Then you need to convert the JSON object into a Javascript/jQuery-compatible object. Afterwards you can convert back to an array
然后您需要将 JSON 对象转换为与 Javascript/jQuery 兼容的对象。之后,您可以转换回数组
$.ajax({
success: function(result) {
jq_json_obj = $.parseJSON(result); //Convert the JSON object to jQuery-compatible
if(typeof jq_json_obj == 'object'){ //Test if variable is a [JSON] object
jq_obj = eval (jq_json_obj);
//Convert back to an array
jq_array = [];
for(elem in jq_obj){
jq_array.push(jq_obj[elem]);
}
console.log(jq_array);
}else{
console.log("Error occurred!");
}
}
});
回答by M.Hemant
$.ajax({
type: 'POST',
url: 'processor.php',//please return in json
dataType : 'json',//to get data in json
data: 'data1=testdata1&data2=testdata2&data3=testdata3',
cache: false,
success: function(result) {
if(result.array.length > 0){
for(var i=0;i<result.array.length;i++){
if(result.array.[i]== 'a'){
//do somthing..
//here, you can use switch case too...
}
}
}
}
});

