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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Ajax call returning status code 0 for a 404 Not Found

  • 08-08-2017 8: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,824 ✭✭✭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,262 ✭✭✭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