Introduction
{readyState:0,status:0,statusText: error}
This article aims to help you diagnose and investigate the error response above. This error occurs when making an AJAX call, and may have multiple causes. However, the error is often due to one of two primary causes.
Investigation:
Cause 1: AJAX Call Cancelled by a Reload or Redirect
This occurs when the AJAX call is triggered at the same time as a browser event that reloads or redirects the page. This causes a race condition where the AJAX call is started but the browser event navigates away from the current page before the call's completion. This causes the AJAX request to be cancelled with the “0” error message.
Triggering events could include the following:
- Form submission
- Clicking on a link
The solution to this depends on how your code is triggering the AJAX call and any other simultaneous events. Generally, make sure of two things:
- If you prompt the AJAX call via a trigger with a default event that causes the page to redirect/reload, make sure to override the default behaviour and have the event wait until the AJAX call is complete before redirecting. Add this behaviour back in to the AJAX completion handler function as per #2.
- If you need to perform any other logic related to reloading/redirecting the page, make sure to add this logic inside the AJAX completion handler. Otherwise, restructure your code so it doesn’t perform this action until the AJAX call is complete.
// You may also want to add this to prevent any default actions (e.g. link redirection) from what triggers the AJAX call e.PreventDefault() $.ajax({ url: '', dataType: 'jsonp', success: function (json) { // success handler }, error: function (error) { // error handler }, complete: function () { **// Add logic to redirect/reload the page here** // Any redirect/reload events outside this function // will cause the AJAX call to be cancelled } });
References:
- Error Diagnosis: jqXHR Returning Readystate 0 and Status 0
- Stack Overflow: Tracking AJAX Error on ReadyState 0, Status 0 and statusText error
- Stack Overflow: jQuery AJAX Error Diagnosis A
- Stack Overflow: jQuery AJAX Error Diagnosis B
- The Art of Simplicity: AJAX request returns status 0
Cause 2: Cross Domain Issues
This occurs when the AJAX function call tries to make a cross-origin request and is blocked by the browser. This is usually easy to diagnose as if it exists, it will cause the AJAX call in the function to fail every time.
The error occurs because due to browser security controls, most AJAX requests are subject to same origin policy.
For solutions, this Stack Overflow Reference has some potential leads. Alternatively, try enabling CORS or making a request via JSONP.