我試圖通過AJAX使用簡單的HTML表單上傳圖像,包括input[type='file']
元素和formData()
,但是當我檢查formData()
時,它看起來是空的,沒有任何內容被傳遞到服務器。使用AJAX上傳文件
jQuery的
//Dynamically loaded form
$(document).on('submit', '.update-form', function(e) {
e.preventDefault();
var form = $(this);
amendDatabase(form);
});
function amendDatabase(form) {
var formData = new FormData(form);
console.log(formData); //This produces 'formData: No properties'
var url = 'file.php';
request = $.ajax({
url: url,
method: 'post',
data: formData,
processData: false,
contentType: false
});
request.done(function(data) {
console.log(data); //This produces an empty/blank return
});
}
file.php
function uploadFile() {
echo 'formData was passed to server';
}
uploadFile();
HTML
<form class="update-form" id="add-file" enctype="multipart/form-data">
<label for="image">Product image</label>
<input type="file" id="image" name="image" multiple="multiple">
<div class="hidden-holder">
<input type="hidden" id="action" name="action" value="file">
</div>
<div class="submit-holder">
<input type="submit">
</div>
</form>
你看過了參數嗎? –
@CharlotteDunois你指的是哪個參數? – Ryan