将数组通过 AJAX 从 php 传递到 javascript

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

Passing array through AJAX from php to javascript

phpjavascriptmysqljquery

提问by user1903898

I need to get an array generated from a script php. I have Latitude and Longitude for each user in a database. I take the values from the db with this code (file.php):

我需要从脚本 php 生成一个数组。我有数据库中每个用户的纬度和经度。我使用此代码(file.php)从数​​据库中获取值:

$query = "SELECT Latitude, Longitude FROM USERS";
$result=mysql_query($query);
$array=array();
while ($data = mysql_fetch_array($result)) {
    $array[]=$data['Latitude'];
    $array[]=$data['Longitude'];
}

echo $array;

and I call with ajax with this code:

我用这个代码用ajax调用:

$.post('./file.php',
     function( result ){    
         alert(result);
     });

but even if in the script php the array is correct (if I echo array[25] I obtain the right value) in Javascript I obtain "Undefined". How can I get the array in correct way?? thanks!

但即使在脚本 php 中数组是正确的(如果我 echo array[25] 我获得了正确的值)在 Javascript 中我获得了“未定义”。如何以正确的方式获取数组?谢谢!

edit: after encoded with json_encode($array); in php and JSON.parse(result) in javascript seems not working. In the console I have the array, but I can't access to its values. (Array[0] gave me "undefined").

编辑:用 json_encode($array) 编码后;在 php 和 JSON.parse(result) 中 javascript 似乎不起作用。在控制台中,我有数组,但我无法访问它的值。(Array[0] 给了我“未定义”)。

回答by Ruslan Polutsygan

use this

用这个

echo json_encode($array);

on server side

在服务器端

and

var arr=JSON.parse(result);

on client side

在客户端

回答by emrox

As Ruslan Polutsygan mentioned, you cen use

正如 Ruslan Polutsygan 提到的,你使用

echo json_encode($array);

on the PHP Side.

在 PHP 端。

On the Javascript-Side you can simply add the DataType to the $.post()-Function:

在 Javascript 端,您可以简单地将 DataType 添加到 $.post()-Function:

$.post(
  './file.php',
  function( result ){    
    console.log(result);
  },
  'json'
);

and the result-Parameter is the parsed JSON-Data.

结果参数是解析后的 JSON 数据。

You can also set the correct Content-Type in your PHP Script. Then jQuery should automaticly parse the JSON Data returned from your PHP Script:

您还可以在 PHP 脚本中设置正确的 Content-Type。然后 jQuery 应该自动解析从你的 PHP 脚本返回的 JSON 数据:

header('Content-type: application/json');

See
http://api.jquery.com/jQuery.post/
http://de3.php.net/json_encode


http://api.jquery.com/jQuery.post/
http://de3.php.net/json_encode

回答by Shikyo

You need to convert the php array to json, try:

您需要将 php 数组转换为 json,请尝试:

echo json_encode($array);

echo json_encode($array);

jQuery should be able to see it's json being returned and create a javascript object out of it automatically.

jQuery 应该能够看到它返回的 json 并自动从中创建一个 javascript 对象。

$.post('./file.php', function(result)
{    
     $.each(result, function()
     {
         console.log(this.Latitude + ":" + this.Longitude);
     });
});