简体   繁体   English

“语法错误:意外的标记 ':'。解析错误。” JSON 和 ajax

[英]"SyntaxError: Unexpected token ':'. Parse error." JSON & ajax

I'm trying to use google maps api with distance matrix api, i'm getting the routes along with distance and time in json format.我正在尝试将谷歌地图 api 与距离矩阵 api 一起使用,我正在以 json 格式获取路线以及距离和时间。

an example of the link i'm getting data from:我从以下链接获取数据的示例:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=place_id:ChIJ7-bxRDmr3oARawtVV_lGLtw&destinations=place_id:ChIJyYB_SZVU2YARR-I1Jjf08F0&key=YOUR_API_KEY&units=imperial https://maps.googleapis.com/maps/api/distancematrix/json?origins=place_id:ChIJ7-bxRDmr3oARawtVV_lGLtw&destinations=place_id:ChIJyYB_SZVU2YARR-I1Jjf08F0&key=YOUR_API_KEY&unit

and data is viewed as follows:和数据查看如下:

{
   "destination_addresses" : [ "2920 Zoo Dr, San Diego, CA 92101, USA" ],
   "origin_addresses" : [
      "San Diego International Airport (SAN), 3225 N Harbor Dr, San Diego, CA 92101, USA"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "5.2 mi",
                  "value" : 8440
               },
               "duration" : {
                  "text" : "13 mins",
                  "value" : 756
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

i'm calling a jquery function on button click (ajax) as follows:我在按钮单击(ajax)上调用 jquery 函数,如下所示:

$.ajax({
        type: 'GET',
        url: 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=place_id:' + me.originPlaceId + "&destinations=place_id:" + me.destinationPlaceId + "&key=YOUR_API_KEY&units=imperial",
        dataType: "JSONP", // data type expected from server
        accepts: 'application/json',
        success: function(data) {
          console.log(data);
        },
        error: function(error) {
          console.log('Error: ' + error);
        }
});

this code is the result of many other posts but i'm still getting the error:此代码是许多其他帖子的结果,但我仍然收到错误消息:

SyntaxError: Unexpected token ':'. Parse error.

in line 2 of the json data.在 json 数据的第 2 行。 This error occurs on safari browser, and if i run the code on chrome i get the following error:此错误发生在 safari 浏览器上,如果我在 chrome 上运行代码,则会出现以下错误:

jquery-3.4.1.min.js:2 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://maps.googleapis.com/maps/api/distancematrix/json?origins=place_id:ChIJ7-bxRDmr3oARawtVV_lGLtw&destinations=place_id:ChIJyYB_SZVU2YARR-I1Jjf08F0&key=AYOUR_API_KEY&units=imperial&callback=jQuery34106919863879807548_1566930061936&_=1566930061937 with MIME type application/json.

I need help solving these errors please, thank you in advance.我需要帮助解决这些错误,在此先感谢您。

Since you are making a Distance Matrix API web service request in the client-side (front-end) that's why you are getting the Cross-origin blocking error (CORB).由于您在客户端(前端)发出距离矩阵 API Web 服务请求,这就是您收到跨源阻塞错误 (CORB) 的原因。 Web service requests are meant to be executed server side . Web 服务请求旨在在服务器端执行。

Note that if you intend to use Distance Matrix in client-side, the JavaScript API has a Distance Matrix Service (which prevents the CORB issue).请注意,如果您打算在客户端使用距离矩阵,JavaScript API 有一个距离矩阵服务(它可以防止 CORB 问题)。 Please refer to this guide: https://developers.google.com/maps/documentation/javascript/distancematrix请参考本指南: https : //developers.google.com/maps/documentation/javascript/distancematrix

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: Unexpected token &lt; in JSON at position 0&quot; 错误在 AJAX - SyntaxError: Unexpected token < in JSON at position 0” error in AJAX JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0` JSON.parse() 错误 SyntaxError: Unexpected token &lt; in JSON at position 0 - JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0 Rails Ajax错误-SyntaxError:JSON中位置0处的意外令牌C - Rails ajax error - SyntaxError: Unexpected token C in JSON at position 0 AJAX发布错误-SyntaxError:JSON位置5出现意外的令牌R - AJAX Post Error - SyntaxError: Unexpected token R in JSON at position 5 AJAX请求错误:“SyntaxError:JSON.parse:unexpected character” - AJAX request error: “SyntaxError: JSON.parse: unexpected character” SyntaxError: JSON.parse: AJAX 调用中出现意外字符错误 - SyntaxError: JSON.parse: unexpected character error in AJAX call Ajax 验证和 SyntaxError: JSON Parse error: Unrecognized token &#39;&lt;&#39; on Laravel - Ajax validation and SyntaxError: JSON Parse error: Unrecognized token '<' on Laravel SyntaxError:意外令牌:json.parse处为T - SyntaxError: Unexpected token: T at json.parse JSON.parse SyntaxError:无效或意外的令牌 - JSON.parse SyntaxError: Invalid or unexpected token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM