Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Ajax call returning status code 0 for a 404 Not Found

Options
  • 08-08-2017 9: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 Posts: 2,789 ✭✭✭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 Posts: 6,250 ✭✭✭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 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