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

回眸只為那壹抹淺笑 提交于 2019-12-23 01:45:08

问题


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:


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



来源:https://stackoverflow.com/questions/34712929/formdata-has-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!