0

Currently, im using the js line below to load website in my HTML page.

window.location = 'http://example.com';

How can I use jQuery / AJAX to load only if http://example.com return HTTP status 200, and display another page if something went wrong like ERROR 500, 504....etc.

JR Galia
  • 17,229
  • 19
  • 92
  • 144

2 Answers2

2

window.location redirects to the specified url, is dosn't load uri into your HTML Page.

According to your question: jQuery Ajax error handling, show custom exception messages shows how to handle ajax errors. In your case the error handling could look like this:

 error: function (xhr, ajaxOptions, thrownError) {
    window.location = 'http://example.com';
  }
Community
  • 1
  • 1
1

This should get you started.

$.ajax({
    url: "http://www.example.com"
}).done(function() {
    alert('done')
}).error(function() {
    alert('error')
});
slinky2000
  • 2,663
  • 1
  • 15
  • 12