(二四)ArcGIS JSAPI 3.x 访问Portal有权限服务

前言

    在server和portal进行联合托管后,我们可以通过portal来管理服务。我们在调用server服务的时候。每次都会出现portal登录页。如下:
(二四)ArcGIS JSAPI 3.x 访问Portal有权限服务_第1张图片

解决思路

  1. 通过ArcGIS Portal中 generateToken来获取token
  2. 通过token来请求服务

实现代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://js.arcgis.com/3.32/esri/css/esri.css">
  <script src="https://js.arcgis.com/3.32/"></script>
</head>
<body>
  <div id="map"></div>
  <script>
    let gConfig = {
      portal_arcgis_hosturl: "https://172.16.0.168/",
      portal_username: "portaladmin",
      portal_pwd: "Gmgtj1234.com",
      ip: "172.26.38.130"
    }

  require([
    "esri/map",
    "esri/layers/ArcGISDynamicMapServiceLayer"
    ], function (Map, ArcGISDynamicMapServiceLayer) {
      const params = {
        'username': gConfig.portal_username,
        'password': gConfig.portal_pwd,
        'referer': gConfig.ip,
        'f': 'json'
      };
      $.ajax({
        type: "POST",
        url: gConfig.portal_arcgis_hosturl + 'portal/sharing/rest/generateToken',
        data: params,
        success: function ({token}) {
          var map = new Map("map");
          let simpleLayer = new ArcGISDynamicMapServiceLayer("https://ghssyrz02.arcgisonline.cn/server/rest/services/sd_2019_xzq/MapServer");
          map.addLayer(simpleLayer);
        }
      }
      )
    });
  </script>
</body>
</html>

你可能感兴趣的:(ArcGIS,JS,学习笔记)