Update Map Service URL?

2164
4
Jump to solution
02-26-2016 02:30 PM
LauraMiles1
Occasional Contributor III

I have a map service ie.

var layerAgreementsOnly = new ArcGISDynamicMapServiceLayer("Census (MapServer) ");

What if I want to change the url? So if I get a security error when loading the map, I would like to switch out the existing url for another one that the user has permission to view. But I'm not sure the syntax or if this is possible.

        layerAgreementsOnly.on("error", function(result){
            var errCode = result.error.code.toString();
            //Test if a 403 security error has been received
            if (errCode.indexOf("403" > -1)){
                
              // code to update url here?

            }
            

               console.log("Error " + errCode + ": " + result.error.message);
        });
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Laura,

  Here is my sample for this:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
  <title>Drop Down Test</title>
  <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
  <style>
    html,
    body,
    #mainWindow {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #FFF;
      overflow: hidden;
      font-family: "Trebuchet MS";
    }
    #header {
      height: 3%;
      overflow: auto;
    }
  </style>
  <script>
    var dojoConfig = {
      parseOnLoad: true
    };
  </script>
  <script src="http://js.arcgis.com/3.10/"></script>
  <script>
    var map;
    require([
      "esri/map",
      "dojo/_base/lang",
      "esri/request",
      "dijit/layout/BorderContainer",
      "dijit/layout/ContentPane",
      "dojo/domReady!"
    ], function(
      Map, lang, esriRequest
    ) {
      map = new Map("map", {
        basemap: "topo",
        center: [-120.1883, 37.0868],
        zoom: 6
      });

      esriRequest({
          url:"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M...",
          content:{
            f:'json'
          },
          handleAs:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(response){
          alert("Data was retrieved");
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));
    });
  </script>
</head>

<body class="claro">
  <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="padding:0px;margin:0px;">
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'left'" style="width:100%;height:100%;border:none;padding:0px;margin:0px;"></div>
  </div>
</body>

</html>

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Laura,

   The way I would handle this is to use an esriRequest with the url like "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M... " to see if you have the permissions to access the layer and if not then add the layer to the map using the other url. This way you are not actually adding anything to the map until you know you have the correct permissions.

LauraMiles1
Occasional Contributor III

Thanks Robert, I think this is just what I was looking for. I have it set up like this

         var secRealEstate = esriRequest({
            url: "myURL",
            timeout: 100 //Set this in milliseconds
        });
              
        secRealEstate.then(requestSucceeded, requestFailed);

       
        function requestSucceeded(){
            alert("Data was retrieved");
        }

        function requestFailed(){
            alert("Data was NOT retrieved");
        }

But even when using the non-secured url, it always comes back with requestFailed. Do you see anything I've done incorrectly? (If I don't set a timeout, it seems my map never reaches the "update-end" event).

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Laura,

  Here is my sample for this:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
  <title>Drop Down Test</title>
  <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
  <style>
    html,
    body,
    #mainWindow {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #FFF;
      overflow: hidden;
      font-family: "Trebuchet MS";
    }
    #header {
      height: 3%;
      overflow: auto;
    }
  </style>
  <script>
    var dojoConfig = {
      parseOnLoad: true
    };
  </script>
  <script src="http://js.arcgis.com/3.10/"></script>
  <script>
    var map;
    require([
      "esri/map",
      "dojo/_base/lang",
      "esri/request",
      "dijit/layout/BorderContainer",
      "dijit/layout/ContentPane",
      "dojo/domReady!"
    ], function(
      Map, lang, esriRequest
    ) {
      map = new Map("map", {
        basemap: "topo",
        center: [-120.1883, 37.0868],
        zoom: 6
      });

      esriRequest({
          url:"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M...",
          content:{
            f:'json'
          },
          handleAs:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(response){
          alert("Data was retrieved");
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));
    });
  </script>
</head>

<body class="claro">
  <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="padding:0px;margin:0px;">
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'left'" style="width:100%;height:100%;border:none;padding:0px;margin:0px;"></div>
  </div>
</body>

</html>
LauraMiles1
Occasional Contributor III

This works - thanks Robert!

0 Kudos