Can Google Maps/Places 'autocomplete' API be used via AJAX?

后端 未结 8 790
广开言路
广开言路 2020-12-14 11:34

I\'m trying to use the Google Places autocomplete API to pre-fill a form on a web application with Establishment data to ease data-entry. The API is pretty straightforward,

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 12:13

    This is a cross domain request. Browsers by default block responses from cross domain sites. You need to use jsonp as the datatyoe. Just google the same and you can see how it can be done using the jquery API. Stack overflow has questions around these too.

    Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML element. Taking advantage of the open policy for elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP.

    Note that Google Places is not JSONP compliant so I used the Google Places JavaScript API instead.

提交回复
热议问题