2011-08-10 8 views
12

아약스 요청을하려고합니다.jquery ajax error { "readyState": 0, "responseText": "", "status": 0, "statusText": "error"}

$.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 "}"

Google에서 검색 한 모든 크로스 사이트 아약스 호출 (이것은 분명히 아닙니다)입니다.

전체 URL을 넣으려고했는데 똑같은 일을합니다.

내가 생각할 수있는 유일한 것은 헤더이고 나는 그것으로 무엇이 잘못 될지 모른다. 다음은 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 

다른 페이지에 다른 요청을 추가했지만 정상적으로 작동하지만이 요청은 다른 요청에 대한 헤더 실패를 계속합니다.

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

왜 '오류'를 조사하지 않습니까? Firebug는 요청 및 응답에 대해 무엇을 알려 줬습니까? –

+0

가짜 양식을 동일한 데이터로 동일한 페이지에 제출하면 어떻게됩니까? –

+0

err이 빈 문자열이고 예 가짜 형식이 제대로 작동하지만이 아약스가 여전히 작동하지 않습니다 –

답변

4

에 변경 : 모든 시간을 등록하는 방법을 사용자가 링크를 클릭합니다.

팝업을 멈추지 않으면 아약스 요청이 완료되지 않고 readyState : 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: " 

을, 하나 파이어 폭스 브라우저가 완벽하게 작동하고 동일한 버전의 다른 파이어 폭스 브라우저가 아약스 호출을하지 않고도이 오류를 제공했다.

해결 방법은 브라우저에서 Adblocker 추가 기능을 비활성화하는 것입니다.

0

내 경우에는 readyState : 0으로 아약스가 실패하게 된 원인을 발견 할 때까지 방금 3 시간의 시간을 낭비했습니다.

문제는 응용 프로그램 캐시의 사용으로 인한 것입니다. 매니페스트 파일에서 나는이 의미있는 오류 메시지없이,로드에서 요청 된 페이지를 방지

Network: *

누락되었습니다. 이것이 아약스가 대부분의 경우 실패하지 않는 이유 일지 모르지만, 같은 실수를 저지른 사람은 적어도 저축하기를 바랍니다.