Before my code runs, I would like to evaluate if a web service is running. How would I do that in JavaScript?
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
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)
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
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.
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.
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'); }, });
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'); }, });
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.
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.