[RESOLVED]check status api

how to check if the api that I intend to consume is available or not before performing requisação of ways to inform the customer that the service is unavailable?

Hi desenv_edilson,

I am not quite sure what do you mean by status of API, but I can tell you how to check if the script that you have loaded in the page is rendered or not like this:

   for (var i = 0; i < document.getElementsByTagName("head")[0].childNodes.length; ++i) {
                if (document.getElementsByTagName("head")[0].children[i].src) alert('The script has been loaded');
            }

So here you can see if the script tag for e.g. 

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

has been loaded into you page.

Please revert if this is not what you want.

Hope it helps though.

I pray this is not au excuse conduct a ajax request and the service that I am consuming is out of service to see it to send an alert to the customer

Hi,

When doing an Ajax call, you can define what to do if the call fails and you could test the http status code to show a different message depending on the kind of error (404, 500, 503 whatever). You are using jQuery ?

Hi desenv_edilson,

For your issue,i suggest that you can check the ajax request status code in the complete function.

Based on the value ,you can alert responding information to customer,please try the code snippet like below:

$.ajax({
    type: "POST",
    dataType: "json",
    data:,
    url: '',
    complete: function(xhr, statusText){
        switch(xhr.status){
            case 200:
                alert("ok");
            case 401:
                alert("invalid credentials");
            case 404:
                alert("xxx")
              ....
            etc
        }
    }
});

Best Regards,

Kevin Shen.

Leave a Reply