How do I evaluate if a web service is running?

7026
22
05-11-2015 08:02 AM
ChrisSergent
Regular Contributor III

Before my code runs, I would like to evaluate if a web service is running. How would I do that in JavaScript?

Tags (2)
0 Kudos
22 Replies
ToddBlanchette
Occasional Contributor II

You can try and catch the error:

<script type="text/javascript">
      var gmap;
  
      function initialize() {
        gmap = new GMap2(document.getElementById("map_canvas"));
        gmap.enableScrollWheelZoom();
        gmap.setCenter(new GLatLng(0, 0), 2);
  
        new esri.arcgis.gmaps.DynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/Unknown_Map_Service/MapServe...", null, 0.75, mapServiceLoaded);
      }
  
      function mapServiceLoaded(layer, error) {
        if (error) {
          // alert the error message to the user and return
          alert("Error " + error.code + ": " + (error.message || (error.details && error.details.join(" ")) || "Unknown error" ));
          return;
        }

        // add layer to the map
        gmap.addOverlay(layer);
      }
    </script>

Cheers,

Todd

ChrisSergent
Regular Contributor III

I am currently not receiving any errors when I pasted in the URL for the stopped web service. Any ideas why?

Here is the stopped service location: test/StreetSignTest (FeatureServer)

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Chris,

   Just checking the html return from that url does not show the error but if you use  http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer?f=json Then you will see error code 500

ChrisSergent
Regular Contributor III

Exactly. I just want to know how to read the value of 500 from my code. Even an alert would help, as I want to exit my code if this code exists.

0 Kudos
SteveCole
Frequent Contributor

I suppose one could use the map's layer-add-result event to populate a boolean variable to log each layer's status. A caveat being that this would only capture any ​initial ​efforts to add the layer. If something goes south after map load, this wouldn't help.

0 Kudos
ChrisSmith7
Frequent Contributor

Chris,

A map service that doesn't exist, or is not running, should return a 500. Would something like this work for you?

$.ajax(serviceURL, {
   type: "POST",
   statusCode: {
      500: function (response) {
         alert('500 error');
      }
   }, success: function () {
      alert('success');
   },
});
0 Kudos
ChrisSmith7
Frequent Contributor

Just to clarify, when I'm referring to the map service throwing a 500 when it doesn't exist, this is assuming the server does exist, e.g. myMapServer/arcgis/rest/services/base_services/1234_does_not_exist/MapServer

Throws a 500 because the server page will still exist (just indicating a status of 500 and that the map service does not exist). I verified in Fiddler the 500 code is still given. You could also add a case for a 404, if needed, so if the server was completely not found:

$.ajax(serviceURL, { 
   type: "POST", 
   statusCode: { 
      404: function (response) { 
         alert('404, not found'); 
      },
      500: function (response) { 
         alert('500 error'); 
      } 
   }, success: function () { 
      alert('success'); 
   }, 
}); 
AndyGup
Esri Regular Contributor

Hey Chris, just some additional clarification for others that may come across your question. HTTP 404 error means not found - no page exists at a particular URL. Where an HTTP 500 error means something happened when the server tried to process the request and it failed. 500 errors can happen for a variety of reasons. Usually there is some additional indication in the 500 error message from ArcGIS Online giving a hint. One example might be you tried to add an attachment to a non-existent ObjectId in a feature service.

ChrisSmith7
Frequent Contributor

Chris mentioned about checking it before his code runs - I wasn't too sure if he meant before his mapping app loaded, or before some bit of code executed in the map app. If it's the latter, it looks like Todd has a better resolution in catching the error.

0 Kudos