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)
问题描述
我正在为我想到的一个项目做一些测试,其中涉及使用附近的地方.所以我和那个大家伙一起去玩弄谷歌的 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


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01