javascript
ArcGIS JS API 4.10跨域问题(本地切片无法加载)解决办法
場景:
? ? ? ?使用ArcGIS API for JavaScript 4.10加載服務器發(fā)布的切片地圖報錯跨域,之前用3.x系列一切正常,換成4.10之后不出圖,關鍵是加載官方在線切片服務一切正常。升到4.10后發(fā)現(xiàn)加載個切片圖層都那么費勁。
報錯內(nèi)容:
解決方法:
? ? ? ?1.下載代理文件,地址:https://github.com/Esri/resource-proxy/releases
提供三種,我這邊用的是ASP.NET版本的,要部署到IIS下面。
? ? ? ?2.部署代理文件,將下載的DotNet文件夾拷貝到IIS對應目錄下,一般都是inetpub/wwwrooot文件夾下面;打開IIS服務管理器,在DotNet文件夾右鍵單擊,選擇轉(zhuǎn)換為應用程序。注意應用池選擇v4.0或以上,如下圖:
? ? ? ?3.測試代理文件是否部署成功,瀏覽器輸入http://localhost/DotNet/proxy.ashx?http://services.arcgisonline.com/ArcGIS/rest/services/?f=pjson? 如果出現(xiàn)下圖,說明部署成功?:
? ? ? 4.修改代理文件,打開DotNet文件夾下的proxy.config,在<serverUrls>中仿照著添加ArcGIS Server 地址,之后一定注意重啟IIS服務,否則無效,我的是下圖:
之后可以用瀏覽器訪問:http://localhost/DotNet/proxy.ashx?http://[IP地址]:6080/arcgis/rest/services 測試一下
? ? ? ? 5.代碼中引入 "esri/core/urlUtils"模塊,添加代碼:
urlUtils.addProxyRule({urlPrefix: "http://111.12.111.11:6080",//配置文件proxy.config中的地址,要請求的地址proxyUrl: "http://localhost/DotNet/proxy.ashx"//部署的代理文件地址});完整代碼:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>切片地圖4.10</title><link rel="stylesheet" href="http://localhost/arcgis_js_v410_api/arcgis_js_api/library/4.10/esri/css/main.css"><style>html, body, #viewDiv{padding: 0;margin: 0;height: 100%;}</style><script src="http://localhost/arcgis_js_v410_api/arcgis_js_api/library/4.10/init.js"></script> </head> <script>require(["esri/Map","esri/layers/TileLayer","esri/Basemap","esri/views/MapView","esri/core/urlUtils"], function (Map,TileLayer,Basemap, MapView,urlUtils) {urlUtils.addProxyRule({urlPrefix: "http://127.0.0.1:6080",proxyUrl: "http://localhost/DotNet/proxy.ashx"});var layer = new TileLayer({// url: "https://services.arcgisonline.com/arcgis/rest/services/World_Terrain_Base/MapServer"url: "http://127.0.0.1:6080/arcgis/rest/services/HD_TEST/HD_TiledMap/MapServer"});var basemap = new Basemap({baseLayers: [layer]});var map = new Map({basemap: basemap});var view = new MapView({container: "viewDiv", // Reference to the scene div created in step 5map: map, // Reference to the map object created before the scene});}); </script> <body><div id="viewDiv"></div> </body> </html>參考地址:https://developers.arcgis.com/javascript/latest/guide/proxies/index.html
總結
以上是生活随笔為你收集整理的ArcGIS JS API 4.10跨域问题(本地切片无法加载)解决办法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java实现栅格数据格式文件读取及加法操
- 下一篇: 基于ArcGIS JS API封装doj