스택 사용자 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의 네트워크 개발자 도구를 사용하여 어디에서나 파일이 전송되는 것을 확인할 수 없습니다. 누구든지 내 코드에서 눈부신 구멍이 보이나요? 감사합니다
'오류 및 "beforesend"함수가 매번 반환됩니다. 어떤 오류가 발생 했습니까? –
콘솔 로그는 오류 기능에서 지정한 내용을 인쇄합니다 : "error function works" – izolate