如何在mapbox中解决Surface API中的CORS问题?(How to work around the CORS issue in Surface API from mapbox?)

编程入门 行业动态 更新时间:2024-10-10 23:23:55
如何在mapbox中解决Surface API中的CORS问题?(How to work around the CORS issue in Surface API from mapbox?)

我试图通过Mapbox使用Surface API来分析两个给定点之间的地形。 我正在采取两点的坐标并向API发送一个AJAX调用,但我仍然坚持臭名昭着的CORS问题。

首先,我试图在其示例中使用Mapbox本身提供的URL:

$('#runTerrainAnalysis').on('click', function(e){ var url = 'https://api.tiles.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?layer=contour&fields=ele&points=-112.084004,36.05322;-112.083914,36.053573;-112.083965,36.053845&access_token=pk.eyJ1Ijoicm9oYW4wNzkzIiwiYSI6IjhFeGVzVzgifQ.MQBzoHJmjH19bXDW0b8nKQ'; $.ajax({ url: url, method: 'GET', success: function(response){ console.log(response); }, error: function(response){ console.log(response); } }); });

跨源请求已阻止:同源策略禁止读取远程资源, 网址为https://api.tiles.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?layer=contour&fields=ele&points=-112.084004 ,36.05322; -112.083914,36.053573; -112.083965,36.053845&access_token = pk.eyJ1Ijoicm9oYW4wNzkzIiwiYSI6IjhFeGVzVzgifQ.MQBzoHJmjH19bXDW0b8nKQ 。 (原因:缺少CORS标题'Access-Control-Allow-Origin')。

如何使这个工作?

I am trying to use the Surface API by Mapbox to analyze the terrain between two given points. I am taking the coordinates of the two points and sending an AJAX call to the API but I am stuck with the infamous CORS issue.

To start with I am trying to use the URL provided by Mapbox itself in its examples:

$('#runTerrainAnalysis').on('click', function(e){ var url = 'https://api.tiles.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?layer=contour&fields=ele&points=-112.084004,36.05322;-112.083914,36.053573;-112.083965,36.053845&access_token=pk.eyJ1Ijoicm9oYW4wNzkzIiwiYSI6IjhFeGVzVzgifQ.MQBzoHJmjH19bXDW0b8nKQ'; $.ajax({ url: url, method: 'GET', success: function(response){ console.log(response); }, error: function(response){ console.log(response); } }); });

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.tiles.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?layer=contour&fields=ele&points=-112.084004,36.05322;-112.083914,36.053573;-112.083965,36.053845&access_token=pk.eyJ1Ijoicm9oYW4wNzkzIiwiYSI6IjhFeGVzVzgifQ.MQBzoHJmjH19bXDW0b8nKQ. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

How do get this to work?

最满意答案

Mapbox API支持没有域限制的跨源请求 ,因此它必须与jquery相关。

尝试包含crossDomain: true :

$.ajax({ url: url, crossDomain: true, method: 'GET',

We can send cross domain AJAX requests using JSONP. Below is the simple JSONP Request:

$.ajax({ url : url, dataType:"jsonp", });

Source

Working like a charm. :)

更多推荐

本文发布于:2023-08-07 17:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Surface   mapbox   API   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!