来自自制php API的ajaxcall上的JSON解析错误

时间:2015-12-21 14:37:46

标签: javascript php jquery json ajax

当我尝试从google distancematrixservices获得响应时,我收到此错误: SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符 如果我从我的API执行jQuery ajax调用:

   <?php
     $key = myKey;
     $from = $_GET['from'];
     $to = $_GET['to'];
     $mode = $_GET['mode'];
     $url = 'https://maps.googleapis.com/maps/api/distancematrix/json…' . urlencode($from) . '&destinations=' . urlencode($to) . '&key='. urlencode($key) .'&travelmode=' . urlencode($mode) . '&departure_time=now';
     $json = file_get_contents($url);
     echo $json;
    ?> 

javascript ajax call:

var getDurationinTraffic=function(){
$.ajax({
  url:'API.php',
  type: 'get',
  dataType:'json',
  contentType:'json',
  data:{
    from: startpoint,
    to: destination,
    mode: 'DRIVING'
  },
  success: function(data, textStatus, jqXHR) {
      console.log(data);
  },
  error : function(jqXHR, textStatus, errorThrown) {
      console.log(errorThrown);
  },
  statusCode: {
      404: function() {
          alert("oops something went wrong");
      }
  }
});

};

我的javascript和API都在我的localhost上运行

0 个答案:

没有答案