最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

I doing some testing for a project I got in mind which involves using places nearby. So I went with the big guy and started messing around with Google's Places Api. I'm using leaflet with openstreet tiles for my map. Now everything is fine until I try to use the dang thing.

var lat = coords.lat;
var lng = coords.lng;
var apiUrl = "";
var data = {
    key: 'AIzaSyBl8bmE8kQT7RjoXhP6k2yDti44h9-fSUI',
    location: lat+','+lng,
    radius: '10000',
    sensor: 'false',
    rankby: 'prominence',
    types: 'bar|night_club'
};
$.ajax({
  url: apiUrl,
  type: 'POST',
  data: data,    
  dataType:"jsonp",
  crossDomain: true,
  success: function(data) {
            var obj = $.parseJSON(data);
                // console.log(data.next_page_token);
          }
});

Changing the dataType property to json I get Origin http://localhost is not allowed by Access-Control-Allow-Origin. Using jsonp I get a parsing error Unexpected token : Obviusly $.parseJSON does not work... Is there a way to make this work without having to use Google Maps Api? If the answer is no... Is there another places api as good as google's?

Thanks!

I doing some testing for a project I got in mind which involves using places nearby. So I went with the big guy and started messing around with Google's Places Api. I'm using leaflet with openstreet tiles for my map. Now everything is fine until I try to use the dang thing.

var lat = coords.lat;
var lng = coords.lng;
var apiUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json";
var data = {
    key: 'AIzaSyBl8bmE8kQT7RjoXhP6k2yDti44h9-fSUI',
    location: lat+','+lng,
    radius: '10000',
    sensor: 'false',
    rankby: 'prominence',
    types: 'bar|night_club'
};
$.ajax({
  url: apiUrl,
  type: 'POST',
  data: data,    
  dataType:"jsonp",
  crossDomain: true,
  success: function(data) {
            var obj = $.parseJSON(data);
                // console.log(data.next_page_token);
          }
});

Changing the dataType property to json I get Origin http://localhost is not allowed by Access-Control-Allow-Origin. Using jsonp I get a parsing error Unexpected token : Obviusly $.parseJSON does not work... Is there a way to make this work without having to use Google Maps Api? If the answer is no... Is there another places api as good as google's?

Thanks!

Share Improve this question edited Feb 2, 2019 at 10:22 sideshowbarker 88.1k29 gold badges214 silver badges211 bronze badges asked Apr 6, 2013 at 6:03 LouieVLouieV 1,0523 gold badges16 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论