未捕获的TypeError:无法构造FormData

时间:2019-09-07 06:57:08

标签: javascript node.js

  

未捕获的TypeError:无法构造'FormData':参数1不是   类型为“ HTMLFormElement”。

这是我的FormData的代码:

var form = document.getElementById('register');
var data = new FormData(form);

function register(data) {
    var url = 'http://localhost:3000/register';

    var config = {

        method : 'post',
        header : {
            'Accept' : 'application/json',
            'Content-Type' : 'application/json'
        },
        body : JSON.stringify(data)
    };

    var takeResponse = function (response) {
        return response.json();
    };

    var finalResponse = function (data) {
        console.log(data);
    }

    fetch(url,config).then(takeResponse).then(finalResponse);

}

function onRegistrationClick(event){
    event.preventDefault();

    var form = document.getElementById("register");
    var data = new FormData(form);

    var _name  = data.get('Name');
    var _email = data.get('Email');
    var _pass  = data.get('Password');

    var params = {
        name     : _name,
        email    : _email,
        password : _pass
    };

    console.log(params);

    if(_name && _email && _pass){
        register(params);
    }
    else{
        alert('Form is not filled up');
    }
}

HTML代码:

<button class="btn btn-success" onclick="onRegistrationClick(event)">Submit</button>

0 个答案:

没有答案