gpt4 book ai didi

javascript - 未经授权的访问-mapbox : the server responded with a status of 401

转载 作者:行者123 更新时间:2023-11-30 14:30:37 25 4
gpt4 key购买 nike

我正在尝试按照传单 map 教程(使用 Mapbox tiles)进行操作,以便在我的网页中基本显示 map 。我确保创建个人 token 并按照建议在 tileLayer 中替换它。但我总是在控制台中收到“加载资源失败:服务器响应状态为 401(未经授权)”错误。

我的代码与教程中的基本相同,只是我添加了生成的 token :

<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"
integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"
integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
crossorigin=""></script>

</head>
<body>

<div id="mapid"></div>
</body>
</html>
<script type="text/javascript">
document.onload = loadMap();

function loadMap() {

var map = L.map('mapid').setView([51.505, -0.09], 13);


L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={sk.eyJ1IjoiaXFhcmNobGkiLCJhIjoiY2pqZTN1OTNxMGNubDNwbXNhbDNka2J4ZiJ9.UKoT12YRTSSUEnIGHyiScQ}', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox-outdoors',
accessToken: 'sk.eyJ1IjoiaXFhcmNobGkiLCJhIjoiY2pqZTN1OTNxMGNubDNwbXNhbDNka2J4ZiJ9.UKoT12YRTSSUEnIGHyiScQ'
}).addTo(map);

}

</script>

最佳答案

您不应该将 token 放在 URL 中。 acces_token={accessToken} 将在 javascript 加载时被替换。你只需要像这里一样指定你的 key :

accessToken:'*切掉 key *'

因此这应该有效:

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox-outdoors',
accessToken: 'sk.eyJ1IjoiaXFhcmNobGkiLCJhIjoiY2pqZTN1OTNxMGNubDNwbXNhbDNka2J4ZiJ9.UKoT12YRTSSUEnIGHyiScQ'
}).addTo(map);

关于javascript - 未经授权的访问-mapbox : the server responded with a status of 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51245945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com