【问题标题】:SyntaxError: Unexpected token t in JSONSyntaxError:JSON 中的意外标记 t
【发布时间】:2017-03-27 11:29:31
【问题描述】:

我正在创建一个 Angular 应用程序,该应用程序从在线可用的 api 获取一些数据,首先我从我的 php 脚本中获取 api 信息,而不是在我的 angularjs 应用程序中获取 json 结果,但给了我“SyntaxError: JSON中的意外标记t“,无法弄清楚原因。

我的 php:

header('Content-Type: application/json');
// jSON URL which should be requested
$json_url = "http://iatageo.com/getLatLng/OPO";

// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options

// Getting results
$result = curl_exec($ch); // Getting jSON result string

echo json_encode($result);

控制器:

 $http.get('iata.php').then(function(response){
        $scope.originLatLong = response.data;
console.log($scope.originLatLong);
    });

【问题讨论】:

  • response 是什么?
  • 304 未修改
  • 您是否尝试解析响应?
  • 试试 $scope.originLatLong = response;
  • 当您尝试解析或编码 json 时会产生这种类型的错误。我怀疑编码时存在一些问题。

标签: javascript php angularjs json angularjs-scope


【解决方案1】:

只需将你的网址粘贴到浏览器中,它就会返回:

{"latitude":"41.2481002808","longitude":"-8.68138980865","code":"OPO"}true

您收到错误,因为您从响应中获取了所有数据,在 JSON 之后带有布尔参数。 因此,请先检查responce var,然后检索您需要的有效数据。

最好的问候。

【讨论】:

  • 你好 Viktor,我已经尝试过给我一个错误“SyntaxError: Unexpected token t in JSON at position 70 at JSON.parse ()”
  • 您的回复不是有效的 JSON,这就是您收到此错误的原因。首先检查响应变量,然后检索正确的子参数。
  • 我认为回复已经更新,因为我再也看不到}true
  • 很奇怪,我仍然认为是真的,我已经 console.log 看到孩子们的响应,但是当我 console.log 响应时它给了我这个错误
  • 如果响应已更新,请提供您的 JSON 响应。
猜你喜欢
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2018-08-30
  • 2021-11-28
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
相关资源
最近更新 更多