Javascript 如何从 AJAX 调用返回数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8823925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:40:55  来源:igfitidea点击:

How to return an array from an AJAX call?

javascriptphpjqueryjsonajax

提问by k_sel

I'm searching for a better solution to making an AJAX call with jQuery, having the PHP file return an array, and have it come out client-side as a Javascript array. Here is what I have been doing this:

我正在寻找更好的解决方案来使用 jQuery 进行 AJAX 调用,让 PHP 文件返回一个数组,并将其作为 Javascript 数组在客户端出现。这是我一直在做的事情:

PHP File (Example.php):

PHP 文件(例如.php):

<?php
    $id_numbers = array('NES-ZL','NES-AL','SNS-ZL');

    for ($i=0; $i<count($the_array); $i++){
        echo $id_numbers[$i];
        echo '|';
    }
?>

JS File:

JS文件:

id_numbers = new Array();
$.ajax({
    url:"Example.php",
    type:"POST",
    success:function(msg){
        id_numbers = msg.split('|');
    }
});

My current method is just a little too convoluted for my taste.

我目前的方法对我的口味来说有点太复杂了。

What I'd like to do is to be able to just

我想做的是能够

return $id_numbers;

on the PHP side and directly translate it to a Javascript array after the AJAX call.

在 PHP 端,并在 AJAX 调用后直接将其转换为 Javascript 数组。

Ideas, anyone?

想法,有人吗?

回答by Alex Turpin

Use JSONto transfer data types (arrays and objects) between client and server.

使用JSON在客户端和服务器之间传输数据类型(数组和对象)。

In PHP:

在 PHP 中:

In JavaScript:

在 JavaScript 中:

PHP:

PHP:

echo json_encode($id_numbers);

JavaScript:

JavaScript:

id_numbers = JSON.parse(msg);

As Wolfgang mentioned, you can give a fourth parameter to jQuery to automatically decode JSON for you.

正如 Wolfgang 提到的,您可以给 jQuery 提供第四个参数来自动为您解码 JSON。

id_numbers = new Array();
$.ajax({
    url:"Example.php",
    type:"POST",
    success:function(msg){
        id_numbers = msg;
    },
    dataType:"json"
});

回答by Wolfgang Stengel

Have a look at json_encode()in PHP. You can get $.ajax to recognize this with the dataType: "json" parameter.

看看PHP中的json_encode()。您可以使用 dataType: "json" 参数让 $.ajax 识别这一点。

回答by swedge218

@Xeon06, nice but just as a fyi for those that read this thread and tried like me... when returning the array from php => json_encode($theArray). converts to a string which to me isn't easy to manipulate esp for soft js users like myself.

@Xeon06,很好,但对于那些阅读此线程并像我一样尝试的人来说,这是一个仅供参考...当从 php => 返回数组时json_encode($theArray)。转换为一个字符串,对我来说,对于像我这样的软 js 用户来说,操作 esp 并不容易。

Inside js, you are trying to get the array values and/or keys of the array u r better off using JSON.parse as in var jsArray = JSON.parse(data)where data is return array from php. the json encoded string is converted to js object that can now be manipulated easily.

在 js 中,您试图获取数组值和/或数组的键,最好使用 JSON.parse,var jsArray = JSON.parse(data)因为数据是从 php 返回的数组。json 编码的字符串转换为 js 对象,现在可以轻松操作。

e.g. foo={one:1, two:2, three:3} - gotten after JSON.parse

例如 foo={one:1, two:2,three:3} - 在 JSON.parse 之后得到

for (key in foo){ console.log("foo["+ key +"]="+ foo[key]) }- prints to ur firebug console. voila!

for (key in foo){ console.log("foo["+ key +"]="+ foo[key]) }- 打印到你的萤火虫控制台。瞧!

回答by TimWolla

Have a look at json_encode(http://php.net/manual/en/function.json-encode.php). It is available as of PHP 5.2. Use the parameter dataType: 'json'to have it parsed for you. You'll have the Object as the first argument in success then. For further information have a look at the jQuery-documentation: http://api.jquery.com/jQuery.ajax/

看看json_encode(http://php.net/manual/en/function.json-encode.php)。它从 PHP 5.2 开始可用。使用参数dataType: 'json'为您解析它。那么你将把 Object 作为成功的第一个参数。有关更多信息,请查看 jQuery 文档:http: //api.jquery.com/jQuery.ajax/

回答by Jamie Hutber

Php has a super sexy function for this, just pass the array to it:

Php 有一个超级性感的功能,只需将数组传递给它:

$json = json_encode($var);

$.ajax({
url:"Example.php",
type:"POST",
dataType : "json",
success:function(msg){
    console.info(msg);
}
});

simples :)

简单:)