formData.has()不是函数 [英] formData.has() is not a function
问题描述
我正在尝试做一个简单的ajax文件上传,但是我收到一个"Uncaught TypeError:formData.has不是一个函数"
I'm trying to do a simple ajax file upload, but I'm getting an "Uncaught TypeError: formData.has is not a function"
如果我也注释掉formData.has()检查函数并仅将其替换为formData.append('myResume'),则会收到类似的错误消息,提示formData.get()不是我的Ajax调用中的函数.有什么建议?谢谢:)
If I also comment out the formData.has() checking function and just replace it with formData.append('myResume'), I get a similar error that says formData.get() is not a function in my ajax call. Any suggestions? Thanks :)
这是html:
<div id="upload">
<form id="file-form" name="resume.pdf">
<input type="file" id="file-select"/>
<button type="submit" id="upload-button">Upload</button>
</form>
和javascript:
and the javascript:
$(function(){
var formData = new FormData();
$('#file-form').submit(function(event){
var fileInput = document.getElementById('file-select').files;
var file = fileInput.item(0);
event.preventDefault();
//Error here formData.has() is not a function
if(formData.has('myResume')){
formData.set('myResume', file);
} else{
formData.append('myResume', file);
}
$.post('/upload', {file: formData.get('myResume')});
})
})
推荐答案
See https://developer.mozilla.org/en-US/docs/Web/API/FormData#Browser_compatibility. It states that for chrome the delete()
, get()
, getAll()
, has()
, set()
methods are supported behind a flag.
这意味着您需要从设置中启用对这些方法的支持( 启用实验性Web平台功能 chrome://flags 中的标志).
This means that you need to enable support for these methods from the settings (Enable experimental Web Platform features flag in chrome://flags).
这篇关于formData.has()不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!