How to catch wrong URL in JavaScript?

Konrad Viltersten

I need to fetch data from the main service but sometimes, it's not working and I wanted to have a fallback. The idea of a Q&D solution was to download some data from it and if it fails, catch the error and change the URL to a local one. Like this.

let found = null;
try {
  $.ajax({ url: mainUrl })
    .done(function (a) { found = true; });
} catch (e) { 
  found = false;
}

In the console, I can see the browser whine about not being able to find the source but the testing variable found is still null. What am I missing?

If there's a better (but still fairly simple) way to detect the presence of the web services? If so, I'm open to suggestions. But mainly, I'm curious what's wrong with the sample above.

James Dilleen

The call is asynchronous, the browser doesn't wait for your ajax to finish before continuing onto the rest of your script (where you check the value of found for example).

Also, the try/catch isn't going to set found to false because the ajax is triggering, it's just failing on the call. Try using .fail() after your .done() to catch a failed request.

This still won't set your found variable to false, though. Have a look here for an explanation much better than I can provide on how to deal with this: https://stackoverflow.com/a/14220323/1843048

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I catch javascript code injection in URL using python?

How to catch the eddystone url

How to catch the invalid URL?

How to catch wrong password/username response in angular?

How to catch wrong username or password on Expressjs?

How to catch if editor url is opened?

how to catch the full url including #

JavaScript: Try/Catch - what am I doing wrong here?

Laravel - How to catch "HttpExceptions", because of POST request when CSFR is wrong?

How to catch std::variant holding wrong type in compile-time?

How does javascript manage to catch exception in late .catch handler?

Catch wrong password

How to redirect to Invalid page if wrong URL or wrong controller/action in Yii?

How to catch current css translate() position in javascript

How try catch order is executed in JavaScript?

How to catch all upper cases in javascript

How to Catch Errors in JavaScript Dropbox Datastore API

How to catch a part of regex but with some requirement in javascript?

How to catch promise runtime Javascript errors?

how to catch an index of an array that iterates (Javascript)

How to make throw in javascript reusable in try catch

Catch the value input field with Javascript and put it in destination URL

Java Jersey: how to change response for wrong url?

Catch wrong input within dict?

Plugin Javascript with Source URL's For Images Using Wrong Directory

How to Change URL With JavaScript

JavaScript triangle wrong direction how to make it correct?

How to catch panic when HTTP client make request to unavailable url?

How to catch id and token from url in Angular 7+