2011-08-10 93 views
12

我試圖做一個Ajax請求jQuery的Ajax錯誤{ 「readyState的」 0 「responseText的」: 「」, 「狀態」:0, 「狀態文本」: 「錯誤」}

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

這請求提醒「{」readyState「:0,」responseText「:」「,」status「:0,」statusText「:」error「}」

我已經在谷歌搜索了所有我想出了一個十字網站ajax調用(這顯然不是)

我已經嘗試把完整的網址,它也做同樣的事情。

我能想到的唯一的事情就是標題,我不知道會出現什麼問題。下面是從螢火蟲

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 

我添加了一個不同的頁面上的其他請求的請求頭和它工作得很好,但這個一直未能標題爲其他請求:

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 
+0

你爲什麼不檢查'err'? Firebug告訴你關於請求和響應的內容? –

+0

當您使用相同的數據將假表單提交到同一頁面時,會發生什麼情況? –

+0

err是一個空白字符串,是一個假表單工作得很好,但是這個ajax仍然不起作用 –

回答

4

我遇到了同樣的問題來了:怎麼每次註冊一個用戶點擊鏈接。

問題是,如果你不停止彈出,ajax請求沒有完成,你準備好了狀態:0!

我做的另一個版本上面,也許是更可讀的(即使更詳細)

/* -------------------------------------------------------------------------- 
* 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(); 
      }; 
     } 
    } 
}); 
1

元素那是在調用這個是一個錨標籤,它會調用它,然後下載一個exe文件,當文件下載對話框強制它會取消這個請求,就好像它正在導航到一個新頁面一樣。

我改成了

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> 
0

我得到了同樣的錯誤:

"readyState: 0" 
"responseText: undefined" 
"status: 0" 
"text status: error" 
"error: " 

在我的情況下,一個火狐瀏覽器工作完美,另一個相同版本的Firefox瀏覽器給我這個錯誤,甚至沒有進行ajax調用。

解決方案是在我的瀏覽器中禁用Adblocker加載項。

0

我剛剛浪費了3個小時的時間,直到我發現導致ajax失敗的原因是readyState:0在我的情況下。

問題是由於使用應用程序緩存。在清單文件我失蹤

Network: *

這被加載防止請求的頁面,沒有任何有意義的錯誤消息。 即使這可能不是大多數情況下ajax失敗的原因,但我希望至少可以挽救一些犯過同樣錯誤的人。

相關問題