Google 的 Places API 和 JQuery 请求 - Access-Control-Allow-Origin 不允许来源 http://localhost

Google#39;s Places API and JQuery request - Origin http://localhost is not allowed by Access-Control-Allow-Origin(Google 的 Places API 和 JQuery 请求 - Access-Control-Allow-Origin 不允许来源 http://localhost)

本文介绍了Google 的 Places API 和 JQuery 请求 - Access-Control-Allow-Origin 不允许来源 http://localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我想到的一个项目做一些测试,其中涉及使用附近的地方.所以我和那个大家伙一起去玩弄谷歌的 Places Api.我正在为我的地图使用带有 openstreet 瓷砖的传单.现在一切都很好,直到我尝试使用该死的东西.

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

dataType 属性更改为 json 我得到 Origin http://localhost is not allowed by Access-Control-Allow-Origin.使用 jsonp 我得到一个解析错误 Unexpected token : Obviusly $.parseJSON does not work...有没有办法让这项工作无需使用 Google Maps Api?如果答案是否定的......还有其他地方的api和google的一样好吗?

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?

谢谢!

推荐答案

您正在尝试使用 Places API 网络服务,用于服务器代码,不支持 JavaScript 所需的 JSONP 输出.

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.

在 JavaScript 中,您需要使用 Places Library 来自 地图 API V3.您不能直接从 JavaScript 或 jQuery 代码中点击 URL.(您可能可能会发现 Places Library 使用的 URL 模式,但服务条款不允许在不通过 API/Library 的情况下直接使用,并且 URL 可能随时更改.)

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

您是否有不想通过 JavaScript 使用 Maps API 的原因?

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

这篇关于Google 的 Places API 和 JQuery 请求 - Access-Control-Allow-Origin 不允许来源 http://localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Google 的 Places API 和 JQuery 请求 - Access-Control-Allow-Origin 不允许来源 http://localhost

基础教程推荐