如何使用Javascript从此链接获取formatted_phone_number?

时间:2012-01-04 13:40:55

标签: javascript ajax

这是我的代码。为什么显示空结果?你能在我的代码中发现任何错误吗?

$(document).ready(function() {
  $.getJSON("https://maps.googleapis.com/maps/api/place/details/json?reference=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=AIzaSyBgWOenfvzi-x7n1Xu-WLQx4ltFkuRNrhQ",
   function(data, textStatus){
      $("#placenamesss").append(data.result[0].formatted_phone_number+'<br/>');
   });
});

这样做是否正确?我如何获得formatted_phone_number

2 个答案:

答案 0 :(得分:2)

您正在进行跨XHR请求,出于安全原因这些请求无效。您将不得不创建一个代理,就像一个小PHP脚本,它将获取信息并将其输出给您。

警告,以下情况应该得到保障!!

您的PHP文件(我们称之为proxy.php)可能如下所示:

<?php 

echo file_get_contents($_GET['url']);

?>

你的jQuery,类似于:

$(document).ready(function() {
    $.getJSON("proxy.php?url=https://maps.googleapis.com/maps/api/place/details/json?reference=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=AIzaSyBgWOenfvzi-x7n1Xu-WLQx4ltFkuRNrhQ", function(data, textStatus){

        $("#placenamesss").append(data.result[0].formatted_phone_number+'<br/>');

    });
});

答案 1 :(得分:0)

尝试访问格式化的电话号码,如下所示:

var number = data.result.formatted_phone_number;

result不是数组,只是另一个对象。

相关问题