Google's Places API and JQuery request - Origin http://localhost is not allowed by Access-Control-Allow-Origin

試著忘記壹切 提交于 2019-11-27 04:53:04

You're trying to use the Places API web service, which is meant for use from server code and does not support the JSONP output you'd need for JavaScript.

In JavaScript, you need to use the Places Library from the Maps API V3. You can't just hit a URL directly from JavaScript or jQuery code. (You could probably discover the URL pattern that the Places Library uses, but the terms of service don't allow direct use without going through the API/Library, and the URL could change at any time.)

Is there a reason you don't want to use the Maps API from JavaScript?

https://github.com/joshtronic/php-googleplaces

Just made this and uploaded it to one of my websites.

<?php
include 'GooglePlaces.php';
include 'GooglePlacesClient.php';
$google_places = new joshtronic\GooglePlaces('your_key');
$google_places->location = array(<your_lat>, <your_lon>);
$google_places->radius   = 800;
$results                 = $google_places->nearbySearch();
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *'); 
echo json_encode($results);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!