2012-07-31 5 views
0

스택 사용자 DannYo의 comment here의 일부 코드를 통합하려고하지만 코드가 작동하지 않는 것 같습니다. 오류 및 "beforesend"함수는 매번 반환됩니다.jQuery/Ajax가 작동하지 않는 HTML 파일 업로드

HTML

<form action="" method="post" enctype="multipart/form-data"> 
    <input type="file" id="file"> 
    <input type="email" id="sender" name="sender" required> 
    <input type="email" id="receiver" name="receiver" required> 
    <input type="text" id="message" name="message"> 
    <button id="send" class="button">Send it</button> 
</form> 

자바 스크립트

$("form").on("submit", function() { 

    var formData = new FormData($('form')[0]); 

    $.ajax({ 
     url: 'upload.php', //server script to process data 
     type: 'POST', 
     xhr: function() { // custom xhr 

      myXhr = $.ajaxSettings.xhr(); 

      if (myXhr.upload) { // check if upload property exists 
       myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload 
      } 

      return myXhr; 

     }, 

     //Ajax events 
     beforeSend: function() { console.log("before send function works"); }, 
     success: function(e) { 
      console.log(e); 
     }, 
     error: function() { console.log("error function works"); }, 

     // Form data 
     data: formData, 

     //Options to tell JQuery not to process data or worry about content-type 
     cache: false, 
     contentType: false, 
     processData: false 
    }); 

    return false; 

}); 

PHP는 테스트 목적으로

upload.php 파일은 <?php echo "success"; ?>

있다

Chrome의 네트워크 개발자 도구를 사용하여 어디에서나 파일이 전송되는 것을 확인할 수 없습니다. 누구든지 내 코드에서 눈부신 구멍이 보이나요? 감사합니다

+0

'오류 및 "beforesend"함수가 매번 반환됩니다. 어떤 오류가 발생 했습니까? –

+0

콘솔 로그는 오류 기능에서 지정한 내용을 인쇄합니다 : "error function works" – izolate

답변

1

오류 기능이 호출되면 그 결과는 그 결과 내에있을 가능성이 큽니다. error function works이 표시되면서 어떤 오류가 반환되는지 확인하십시오.

error: function(xhr, status, error) { 
    alert(xhr.responseText); 
    // OR 
    alert(status + " : " + error); 
} 

편집 : 또한 참고로, 나는 당신이 업로드하려는 파일의 저장을 처리해야 믿습니다. T his tutorial for uploading files in php이 유용 할 수 있습니다.

+0

내 오류 기능이 어리석은 데 동의합니다. 당신이 내게 준 것을 사용하면 페이지를 새로 고치지 만 콘솔에 이것을 표시하기 전에는 안됩니다 :'Uncaught TypeError : 준비 할 수 없습니다 'Message of'undefined' – izolate

+1

@muppethead 그게 재미 있습니다. 나는 이전에 해본 다른 대답으로 나의 대답을 업데이트했다. –

+0

아 하! 예, 귀하의 편집이 도움이되었음을 입증했습니다. 오류 'progressHandlingFunction' 정의되지 않았습니다 말했다. 그래서, 진행 함수를 추가하고 이제 오류 함수를 반환하지 않습니다. 하지만 대신 500 내부 서버 오류가 발생합니다. 내가 MAMP와 함께 일하기 때문에 그런가? – izolate

관련 문제