php 如何通过ajax php调用返回包含json对象的数组/json对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19036994/
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 can I return an array/json object containing json objects through an ajax php call?
提问by wizzkid
Basically what I'm trying to do is returning the results of a mysql query. I know how to put each row of the query results into its own JSON object, now I'm just struggling with a way so that if there's multiple lines of results to return it to my jquery. In my jquery I call the $.ajax() function and I don't have any issues with that. My problem lies within the success part, where I want to be able to do something like the following:
基本上我想要做的是返回 mysql 查询的结果。我知道如何将查询结果的每一行放入它自己的 JSON 对象中,现在我只是在努力寻找一种方法,以便如果有多行结果将其返回给我的 jquery。在我的 jquery 中,我调用了 $.ajax() 函数,我对此没有任何问题。我的问题在于成功部分,我希望能够执行以下操作:
$.ajax ({
type: "POST",
url:"select.php",
data: {columns : "*",
table : "tbUsers",
conditions : "" },
success: function(results) {
foreach (results as obj)
{
JSON.parse(obj);
$("#page").html(obj.id + " " + obj.name);
}
}
});
I want to be able to iterate through the result variable like an array of JSON objects. The results variable is a string that consists of All the output of the php file. So let my question rather then be, how can I change it so that the function gets an array or how do I change it into one?
我希望能够像 JSON 对象数组一样遍历结果变量。结果变量是一个字符串,由 php 文件的所有输出组成。所以让我的问题而不是,我怎样才能改变它以便函数获得一个数组或者我如何将它变成一个?
My php file currently returns something like this:
我的 php 文件当前返回如下内容:
[{"0":1, "1":"name1", "id":1, "name":"name1"} , {"0":2, "1":"name2", "id":2, "name":"name2"}]
回答by The Alpha
From the php
you can use
从php
你可以使用
echo json_encode($result); // result may contain multiple rows
In your success
callback you can use
在您的success
回调中,您可以使用
success: function(results) {
var htmlStr = '';
$.each(results, function(k, v){
htmlStr += v.id + ' ' + v.name + '<br />';
});
$("#page").html(htmlStr);
}
回答by Ricky S
Try something like:
尝试类似:
$.ajax ({
type: "POST",
url:"select.php",
data: {columns : "*",
table : "tbUsers",
conditions : "" },
dataType: "json",
success: function(results) {
for( var i in results) {
$("#page").html(results[i].id + " " + results[i].name);
}
}
});
Note the dataType: "json" - This will parse it all into a JSON object(s) for you.
请注意 dataType: "json" - 这将为您将其全部解析为 JSON 对象。
回答by kasper Taeymans
quote from your question
引用你的问题
I know how to put each row of the query results into its ownJSON object
我知道如何将查询结果的每一行放入它自己的JSON 对象中
you need to send one big json string instead of multiple smaller ones. You'll not able to loop through the response because it is not a single json string (it are multiple json strings).
您需要发送一个大的 json 字符串而不是多个较小的字符串。您将无法遍历响应,因为它不是单个 json 字符串(它是多个 json 字符串)。
also it is better to chain the ajax callbacks because using them as options will be removed from jquery in the future.
最好链接 ajax 回调,因为使用它们作为选项将在将来从 jquery 中删除。
$.ajax({
url: ' your/url ',
type: 'POST',
dataType: 'json',
data: {param1: 'value1'},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调从 jQuery 1.8 开始弃用。要为最终删除代码做好准备,请改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。
回答by Alexander Mistakidis
You can just return one big JSON result. Because each of your JSON objects can be wrapped in another.
您可以只返回一个大的 JSON 结果。因为您的每个 JSON 对象都可以包装在另一个中。
The JSON you return would then be an array of objects (whatever they may be)
您返回的 JSON 将是一个对象数组(无论它们是什么)
{ "objects": [(first object), (second object), ... ] }
Then in your success function, you can iterate over each object and update the page:
然后在您的成功函数中,您可以遍历每个对象并更新页面:
var obj = JSON.parse(results);
jQuery.each(objs, function(i, obj) {
$("#page").html(obj.id + " " + obj.name);
});
回答by nanthan
return Because wrapped success Then in your success function,you can iterate over each object and update the page You can just return one big JSON result.Because each of your JSON objects can be wrapped in another. The JSON you return would then be an array of objects (whatever they may be)
return 因为包装成功 然后在您的成功函数中,您可以遍历每个对象并更新页面 您可以只返回一个大的 JSON 结果。因为您的每个 JSON 对象都可以包装在另一个中。您返回的 JSON 将是一个对象数组(无论它们是什么)