2015-02-06 50 views
0

我使用Google Place API獲取最近位置的建議。Google Place API - jQuery JSON接收數據的持有

我設法從服務器獲取數據。

我從服務器像這個 -

{ 
    "predictions" : [ 
     { 
     "description" : "Australia", 
     "id" : "3ba963f8de67dc16df0a8de60e46418338a3181a", 
     "matched_substrings" : [ 
      { 
       "length" : 1, 
       "offset" : 0 
      } 
     ], 
     "place_id" : "ChIJ38WHZwf9KysRUhNblaFnglM", 
     "reference" : "CjQhAAAAlf04PlAgVJSzXXBUwZc0JGNjk5I9hsWHkvWwfuY_U4bkR3hm2lC4EN6fV4KOXr9PEhD5vhvmJOucMwDiwRaYJ0RAGhQofmdPwvnV4qHFefKAu0Anw110WQ", 
     "terms" : [ 
      { 
       "offset" : 0, 
       "value" : "Australia" 
      } 
     ], 
     "types" : [ "country", "political", "geocode" ] 
     }, 
     { 
     "description" : "Argentina", 
     "id" : "dcaa0dffd352dfaaa3dc73dd1dbf3153708637ef", 
     "matched_substrings" : [ 
      { 
       "length" : 1, 
       "offset" : 0 
      } 
     ], 
     "place_id" : "ChIJZ8b99fXKvJURqA_wKpl3Lz0", 
     "reference" : "CjQhAAAAO1RnJcq4Dky5uLQFHCULfP6VzbklXYCiR_DDuJMJxf5wFbTXVnHM7bn7ZSlxsR7IEhC_HlGxn8JeuC2h86S2EIpSGhRddclDM07Acre3NSiTWJoClMNtKQ", 
     "terms" : [ 
      { 
       "offset" : 0, 
       "value" : "Argentina" 
      } 
     ], 
     "types" : [ "country", "political", "geocode" ] 
     }, 
     { 
     "description" : "Austria", 
     "id" : "e16f8e9f2a60f60a0881c41488ff3869e4f938a4", 
     "matched_substrings" : [ 
      { 
       "length" : 1, 
       "offset" : 0 
      } 
     ], 
     "place_id" : "ChIJfyqdJZsHbUcRr8Hk3XvUEhA", 
     "reference" : "CiQfAAAAXX_lF6oxms4MgmajAkQabO3drGaCf04EHArvaGV3UTISEFulfBYy0rmrSxPIb1JLY_0aFJ2UqN170fR7OjOGAF3v3vZqO21i", 
     "terms" : [ 
      { 
       "offset" : 0, 
       "value" : "Austria" 
      } 
     ], 
     "types" : [ "country", "political", "geocode" ] 
     } 
    ], 
    "status" : "OK" 
} 

我所做的與jQuery的解析來獲取數據的迴應是 -

$.ajax(
{ 
    url: Auto_Complete_Link, 
    type: "GET", 
    dataType: 'jsonp', 
    cache: false, 
    crossDomain: true, 
    /*data: JSON.stringify(somejson),*/ 
    success: function (response) 
    { 
     $.each(response, function(i, field) 
     { 
      console.log(field + " <=> "+ i); 
     }); 
    }, 
    error: function (xhr, status) 
    { 
     console.error("not connecting to google server"); 
    } 
}); 

因此,代碼的解析部分喜歡它 -

$.each(response, function(i, field) 
{ 
    console.log(field + " <=> "+ i); 
}); 

但我得到了一個名爲

錯誤

Uncaught SyntaxError: Unexpected token :

錯誤是JSON喜歡它 -

22

任何人都可以請幫助解決這個問題?

在此先感謝您的幫助。

+0

你積極的谷歌Places API的是返回JSONP而不是JSON? – timothyclifford 2015-02-06 15:14:46

+0

我收到的是放在這裏。 – 2015-02-06 15:18:30

+0

我認爲,這是JSONP – 2015-02-06 15:19:24

回答

1

試着改變你的JavaScript來:

$.ajax(
{ 
    url: Auto_Complete_Link, 
    type: "GET", 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    cache: false, 
    crossDomain: true, 
    success: function (response) 
    { 
     $.each(response, function(i, field) 
     { 
      console.log(field + " <=> "+ i); 
     }); 
    }, 
    error: function (xhr, status) 
    { 
     console.error("not connecting to google server"); 
    } 
});