如何通过ajax php调用返回包含json对象的数组/ json对象?

时间:2022-01-07 01:22:29

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"}]

5 个解决方案

#1


10  

From the php you can use

从PHP你可以使用

echo json_encode($result); // result may contain multiple rows

In your success callback you can use

在您的成功回调中,您可以使用

success: function(results) {
    var htmlStr = '';
    $.each(results, function(k, v){
        htmlStr += v.id + ' ' + v.name + '<br />';
   });
   $("#page").html(htmlStr);
}

A Demo to help you understand.

一个演示,以帮助您理解。

#2


3  

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对象。

#3


0  

quote from your question

引用你的问题

I know how to put each row of the query results into its own JSON 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.

弃用注意:自jQuery 1.8起,不推荐使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。要准备最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

#4


0  

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);
});

#5


-1  

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)

返回因为包装成功然后在您的成功函数中,您可以迭代每个对象并更新页面您只需返回一个大的JSON结果。因为每个JSON对象都可以包装在另一个中。您返回的JSON将是一个对象数组(无论它们是什么)

#1


10  

From the php you can use

从PHP你可以使用

echo json_encode($result); // result may contain multiple rows

In your success callback you can use

在您的成功回调中,您可以使用

success: function(results) {
    var htmlStr = '';
    $.each(results, function(k, v){
        htmlStr += v.id + ' ' + v.name + '<br />';
   });
   $("#page").html(htmlStr);
}

A Demo to help you understand.

一个演示,以帮助您理解。

#2


3  

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对象。

#3


0  

quote from your question

引用你的问题

I know how to put each row of the query results into its own JSON 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.

弃用注意:自jQuery 1.8起,不推荐使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。要准备最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

#4


0  

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);
});

#5


-1  

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)

返回因为包装成功然后在您的成功函数中,您可以迭代每个对象并更新页面您只需返回一个大的JSON结果。因为每个JSON对象都可以包装在另一个中。您返回的JSON将是一个对象数组(无论它们是什么)