Jquery Ajax Readystate 0 Responsetext Status 0 Statustext Error

jquery ajax readystate 0 responsetext status 0 statustext error

I was getting this error and in my case it was not due to same origin policy. I got some help from this link. Other possible reasons can be seen in here.

My case was, I had a link button and I was not using e.PreventDefault()

ASPX

<asp:LinkButton ID="lnkSearch" runat="server" CssClass="DockCmdSearch" CommandName="Search" OnClientClick="return VerifySearch(this, event);" />

Javascript

function VerifySearch(sender, e) {        
e.preventDefault();
$.ajax({
type: 'POST',
.............
}
return false;
}

jquery ajax error {"readyState":0,"responseText":"","status":0,"statusText":"error"}

I came across the same issue: how to register every time a user click the link.

The problem is infact that if you don't stop the pop up, the ajax request doesn't complete and you get readyState: 0!

I've done another version of the above that maybe is more readable (even if more verbose)

/*  --------------------------------------------------------------------------
* Before that add 'downloads' class to every anchor tag (link) in your page
* This script does the rest
*
* remember to change 'your_php_file' with the one you use
* -------------------------------------------------------------------------- */

$(document).ready( function()
{

// Check if there is any link with class 'downloads'
if ( typeof $('.downloads') != 'undefined' )
{
var links = $('.downloads');

// Run this for every download link
for ( var i = 0; i < links.length; i++ )
{
// Set click behaviour
links[i].onclick = function(e)
{
// Get download name
var attr = this.attributes,
href = attr.href.textContent,
elem = href.split('/'),
elem = elem[elem.length - 1];

// Send the download file name and only after completing the request let the user download the file
$.ajax(
{
type : "POST",
dataType : "text",
// 'your_php_file' must be an ABSOLUT or RELATIVE path!
url: your_php_file,
// 'elem' is a variable containing the download name
// you can call it in your php file through $_POST['download_name']
data: { download_name: elem },
// here we go magic:
// after the request is done run the popup for the download
complete: function()
{
window.location.href = href;
}
});

// Stop default behaviour until ajax request has been done
e.preventDefault();
};
}
}
});

How to fix ' {"readyState":0,"status":0,"statusText":"error"}' for Ajax GET request

Your Access-Control-Allow-Origin header is fine:

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Mon, 15 Jul 2019 22:34:45 GMT
Content-Type: application/json
Content-Length: 26
Connection: keep-alive
Vary: Accept-Encoding
Content-Security-Policy: frame-ancestors 'none'
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Cookie, Set-Cookie, Authorization
Access-Control-Allow-Credentials: true
Cache-Control: public, max-age=10

{"name":"john","age":"33"}

I suspect that the page you're loading this on is HTTPS, but your API service only supports HTTP. I can reproduce the issue via JSFiddle. (Link in the comments, because Stack Overflow won't let me put it here.)

When you have a page that uses HTTPS, any XHR or Fetch requests must also use HTTPS. Your service only responds to HTTP, which causes this error to occur.

To fix it, enable HTTPS on your API service as well.



Related Topics



Leave a reply



Submit