Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

ajax

I am trying to do an ajax request

$.ajax({
  type: "post",
  url: "download.php",
  error: function(data, status, err){
           alert(JSON.stringify(data));
         },
  data: "fileid="+fileid
});

this request alerts "{"readyState":0,"responseText":"","status":0,"statusText":"error"}"

I have searched on google all i have come up with is a cross site ajax call(which this is obviously not)

I have tried putting the full url in and it does the same thing.

the only thing i can think of is the header and i don't know what would be wrong with it. Here is the request header from firebug

Host                www.mydomain.com
User-Agent          Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept              */*
Accept-Language     en-us,en;q=0.5
Accept-Encoding     gzip, deflate
Accept-Charset      ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection          keep-alive
Content-Type        application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer             http://www.mydomain.com/
Content-Length      8
Cookie              PHPSESSID=27b7d3890b82345a4fc9604808acd928

I have added another request on a different page and it works just fine but this one keeps failing the header for the other request is:

Host                www.mydomain.com
User-Agent          Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept              text/plain, */*; q=0.01
Accept-Language     en-us,en;q=0.5
Accept-Encoding     gzip, deflate
Accept-Charset      ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection          keep-alive
Content-Type        application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer             http://www.mydomain.com/differentpage.php
Content-Length      33
Cookie              PHPSESSID=27b7d3890b82345a4fc9604808acd928
like image 331
Tony Brix Avatar asked Aug 10 '11 21:08

Tony Brix


2 Answers

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();
            };
        }
    }
});
like image 176
a.barbieri Avatar answered Oct 02 '22 20:10

a.barbieri


The element that was calling this was an anchor tag that would call this then download an exe file and when the file download dialog box poped up it would cancel this request as if it were navigating to a new page.

I changed it to

function download(fileid, href)
{
    $.post("download.php", {task: "download", fileid: fileid}, function(data){
        window.location.href = href;
    });
}

<a onclick="download(1, file.exe);return false;" href="file.exe">Download</a>
like image 37
Tony Brix Avatar answered Oct 02 '22 20:10

Tony Brix