Question

I'm using the following code to get some data from an external website.

<script type="text/javascript">
    xui.ready(function() { 

             var url = 'http://www.domain.com/getData.aspx';
             x$().xhr(url,function(){
                 alert(this.responseText);
             });    
        }
    );
</script>

UPDATED:

I updated my code to the following but even if I get a 404, the callback function still fired and not the error function.

<script type="text/javascript">
    xui.ready(function() { 

             var url = 'http://localhost:49617/SalesForceWebservice/Test/Default4.aspx';
             x$().xhr(url,{
                 method: 'get',
                 async: true,
                 error: function(){
                     alert('error');
                 },
                 callback: function(){
                     alert('success');
                 }
             });
        }
    );

</script>

The above code work just fine but this morning, the external website was down and this was causing the page to fail. Based on the documentation, it looks like the callback function is called only when the xhr called received status 200. What is the best way to handle error 500 or 404?

Was it helpful?

Solution

Have you read the documentation ?

Take a look at the {options} you can pass as an argument:

method String can be get, put, delete, post. default is get.

async Boolean enables an asynchronous request. defaults to false.

data String is a url encoded string of parameters to send.

error Function is called on error or status that is not 200. (i.e. failure callback).

callback Function is called on status 200 (i.e. success callback).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top