当我直接从ESRI Leaflet 网页获取此代码并将其放入JSFiddle 时,效果很好。ArcGIS 服务器中的所有图层都会显示出来。
但是,当我将这个确切的代码粘贴到文本文件中并用它制作一个 HTML 页面时,然后用 Chrome、Explorer 等打开它。没有任何经过身份验证的 ArcGIS 图层出现,只有底图。为什么会这样?
<html>
<head>
<meta charset="utf-8" />
<title>ArcGIS Server username/password</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<!-- Load Leaflet from CDN -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<!-- Load Esri Leaflet from CDN -->
<script src="https://unpkg.com/esri-leaflet@2.4.1/dist/esri-leaflet.js"
integrity="sha512-xY2smLIHKirD03vHKDJ2u4pqeHA7OQZZ27EjtqmuhDguxiUvdsOuXMwkg16PQrm9cgTmXtoxA6kwr8KBy3cdcw=="
crossorigin=""></script>
<style>
body { margin:0; padding:0; }
#map { position: absolute; top:0; bottom:0; right:0; left:0; }
</style>
</head>
<body>
<div id="map"></div>
<script>
// workaround for old ie
if (!window.location.origin) {
window.location.origin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
}
var map = L.map('map').setView([34.089, -116.865], 9);
L.esri.basemapLayer('Topographic').addTo(map);
function serverAuth (callback) {
L.esri.post('https://sampleserver6.arcgisonline.com/arcgis/tokens/generateToken', {
username: 'user1',
password: 'user1',
f: 'json',
expiration: 86400,
client: 'referer',
referer: window.location.origin
}, callback);
}
serverAuth(function (error, response) {
if (error) {
return;
}
var dl = L.esri.dynamicMapLayer({
url: 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire_secure_ac/MapServer',
opacity: 1,
token: response.token
}).addTo(map);
dl.on('authenticationrequired', function (e) {
serverAuth(function (error, response) {
if (error) {
return;
}
e.authenticate(response.token);
});
});
});
</script>
</body>
</html>