Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Ajax call returning status code 0 for a 404 Not Found

  • 08-08-2017 09:16PM
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    I have a webpage that calls a rest endpoint using Ajax. I want to print out the http status code when my rest call errors. When the rest endpoint returns a 500, I can see 500 being printed. But when the call returns a 404, then the value 0 is printed. thrownError is blank.

    I'm new to Javascript but can anyone tell me why 0 would be printed? It's only for 404s. I've tested 5xx and they are all fine.
    $.ajax({
        type: 'GET',
        url: 'mysite.com',
        crossDomain: true
        success: function (data) {
          console.log(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(xhr.status);
        },
      });
    


Comments

  • Registered Users, Registered Users 2 Posts: 2,927 ✭✭✭mightyreds


    Are you shutting down the server when you try the call? 0 can mean connection refused too

    Also when the ajax call has been aborted due to browser refresh or similar?

    Basically when there's an error in the call


  • Registered Users, Registered Users 2 Posts: 6,271 ✭✭✭Buford T Justice


    I have a webpage that calls a rest endpoint using Ajax. I want to print out the http status code when my rest call errors. When the rest endpoint returns a 500, I can see 500 being printed. But when the call returns a 404, then the value 0 is printed. thrownError is blank.

    I'm new to Javascript but can anyone tell me why 0 would be printed? It's only for 404s. I've tested 5xx and they are all fine.
    $.ajax({
        type: 'GET',
        url: 'mysite.com',
        crossDomain: true
        success: function (data) {
          console.log(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(xhr.status);
        },
      });
    

    Have you checked your console to see the error? Chrome dev tools have a network tab, and a console that can give more info


  • Registered Users, Registered Users 2 Posts: 705 ✭✭✭cintec


    Do you have a link to the endpoint you are trying to hit or is it your own backend api?
    You can always try a get request using postman to make sure that the url is correct and returning data.

    You can use this endpoint to test your application it just returns weather information.
    http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1


Advertisement