【问题标题】:Send array from php to javascript将数组从 php 发送到 javascript
【发布时间】:2012-12-14 09:58:19
【问题描述】:

我正在使用 php 脚本在屏幕上抓取一个站点,该脚本最终会创建一个数组,我想将它发送回 javascript 调用函数。在下面的代码中,我尝试使用“print_r”将其打印出来,这根本没有给我任何结果(?)。如果我在元素上回显(例如 $addresses[1]),则会显示该元素。

那么,为什么我没有从 php 函数中得到任何东西,以及将数组发送回调用 js 函数的最佳方式是什么?

提前非常感谢!

js:

$.post( 
   "./php/foo.php",
   {
    zipcode: zipcode
   },
  function(data) {
     $('#showData').html(data);
  }
);

php:

$tempAddresses = array();
$addresses = array();

$url = 'http://www.foo.com/addresses/result.jspv?pnr=' . $zipcode;

$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    $cell = $row->find('td', 0);

    array_push($tempAddresses, $cell);
}

$tempAddresses = array_unique($tempAddresses);

foreach ($tempAddresses as $address) {
    array_push($addresses, $address);
}

print_r($addresses);

【问题讨论】:

标签: php jquery arrays post send


【解决方案1】:

您可以使用 JSON 将数组返回到客户端,它可以通过 AJAX 发送,就像您在现有代码上所做的一样。

使用 PHP 的json_encode(),此函数会将您的 PHP 数组转换为 JSON 字符串,您可以使用它通过 AJAX 将其发送回您的客户端

在你的 PHP 代码中(只是为了演示它是如何工作的)

json.php

<?php
$addresses['hello'] = NULL;
 $addresses['hello2'] = NULL;
if($_POST['zipcode'] == '123'){ //your POST data is recieved in a common way
  //sample array
  $addresses['hello'] = 'hi';
  $addresses['hello2'] = 'konnichiwa';
}
else{
   $addresses['hello'] = 'who are you?';
   $addresses['hello2'] = 'dare desu ka';
} 
 echo json_encode($addresses);  
?>

然后在您的客户端脚本中(最好使用 Jquery 的长 AJAX 方式)

$.ajax({
     url:'http://localhost/json.php',
     type:'post',
     dataType:'json',
     data:{ 
         zipcode: '123' //sample data to send to the server
     }, 
     //the variable 'data' contains the response that can be manipulated  in JS 
     success:function(data) { 
          console.log(data); //it would show the JSON array in your console
          alert(data.hello); //will alert "hi"
     }
});

参考

http://api.jquery.com/jQuery.ajax/

http://php.net/manual/en/function.json-encode.php

http://json.org/

【讨论】:

  • 谢谢。试过了,但我只是让 js 函数返回 null :/
  • 我在我身边做了同样的事情,它工作正常。请仔细检查 PHP 代码。
  • 无论如何我都怀疑你的网址,请确保根据你如何使用网络浏览器访问 php 文件进行更正
  • 不,这与从 $.post 到 $.ajax 的更改有关。 $.post 有效,$.ajax 无效:/
  • 好的,我仔细检查了我的代码,我忘记包含您将使用 $.ajax 方法发送到服务器的数据,刚刚修改了我的答案
【解决方案2】:

js应该是

$.ajax({
     url:'your url',
     type:'post',
     dataType:'json',
     success:function(data) {
      console.log(JSON.stringify(data));
     }
    });

服务器

$tempAddresses = array();
$addresses = array();

$url = 'http://www.foo.com/addresses/result.jspv?pnr=' . $zipcode;

$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    $cell = $row->find('td', 0);

    array_push($tempAddresses, $cell);
}

$tempAddresses = array_unique($tempAddresses);

foreach ($tempAddresses as $address) {
    $arr_res[] =$address;
}
header('content-type:application/json');
echo json_encode($arr_res);

【讨论】:

  • JSON.parse 代替 JSON.stringify
  • @fireeyedboy JSON.stringify 它将结果显示为控制台中的字符串,否则显示为对象,仅用于数据的来源
猜你喜欢
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
相关资源
最近更新 更多