How to detect a map service status

4167
4
04-05-2016 12:38 PM
RobertKirkwood
Occasional Contributor III

I have several outside services that i use in my map. Sometimes these services may be down and it affects the loading of my map. I would like to check on the status of the services and if they are down the application would not put them in the map and TOC. I am using the ESRI JavaScript API. Thanks!

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Robert,

  You can use esriRequest to get the services json and if that fails then the map service is dwon and you do not attempt to load the layer.

require([
  "esri/request", ... 
], function(esriRequest, ... ) {
  var layerUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/la...";
  var layersRequest = esriRequest({
    url: layerUrl,
    content: { f: "json" },
    handleAs: "json",
    callbackParamName: "callback"
  });
  layersRequest.then(
    function(response) {
      console.log("Success: ", response.layers);
  }, function(error) {
      console.log("Error: ", error.message);
  });
  ...
});
RobertKirkwood
Occasional Contributor III

Thanks. This gets me going in the right direction!

0 Kudos
RobertKirkwood
Occasional Contributor III

I got this to work for me thanks. I also had to modify the TOC code as follows:

//Code for the  layers in the TOC

        var myLayers = map.getLayer("myLayers ");

        if (myLayers !== undefined) {

            myLayers .setVisibility(false);

            myLayers .setOpacity(0.7);      

            tocEORI = new TOC({

                map: map,

                layerInfos: [{

                        layer: myLayers ,

                        title: "Enhanced Oil Recovery Institute",

                        collapsed: true,

                        slider: false

                    }

                ]

            }, 'tocmyLayers Div');

            tocmyLayers .startup();

        }

        else {

            alert("The service is not available. Please refresh at a later time. This will not affect the overall functionality of the map.");

        }

KenBuja
MVP Esteemed Contributor

This post had another example of using XMLHttpRequest to test this map server response: https://community.esri.com/thread/173645#comment-594343