formData.has() is not a function [duplicate]

2019-06-05 01:24发布

This question already has an answer here:

I'm trying to do a simple ajax file upload, but I'm getting an "Uncaught TypeError: formData.has is not a function"

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 :)

Here's the 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>

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')});
    })
})

1条回答
淡お忘
2楼-- · 2019-06-05 01:52

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.

This means that you need to enable support for these methods from the settings (Enable experimental Web Platform features flag in chrome://flags).

查看更多
登录 后发表回答