从html表单获取数据时出现错误409

时间:2019-06-18 18:57:08

标签: javascript html json forms

我遇到以下问题:我有一个HTML文件,该文件使用一种形式来接收系统中用户注册表的数据。我通过JavaScript获得了这些值,并对API进行了提取,该API会进行注册并展开所有其余的地籍过程。但是,当我进行注册时,遇到了错误409,在我的代码中找不到此错误的原因。您还能找到/解决吗?

注册表格

    <form id="register" name="register" onsubmit="submitRegister(); return false;">
        <h4>Cadastre-se</h4>
            <div>
                <input type="text" placeholder="Primeiro Nome" id="register-fName" required>
                <input type="text" placeholder="Último Nome" id="register-lName" required>
                <input type="email" placeholder="Email" id="register-email" required>
                <input type="password" placeholder="Senha" id="register-psw" required>
                <button type="submit">Cadastrar-se</button>
            </div>
    </form>

JAVASCRIPT功能

    function submitRegister() {
        var fName = document.getElementById("register-fName").value  
        var lname = document.getElementById("register-lName").value  
        var email = document.getElementById("register-email").value  
        var senha = document.getElementById("register-psw").value

        var data = {
            email: email,
            senha: senha,
            primeiroNome: fName,
            ultimoNome: lname

        } 

        fetch('https://api-ucdb.herokuapp.com/api/v1/users/', {
            method: 'POST',
            headers: {
                        'Access-Control-Allow-Origin': '*',
                        'Content-Type': 'application/json; charset=utf-8'
                     },
            body: JSON.stringify(data)
        })
        .then(function(response){
            if (!response.ok)
                throw new Error("Não foi possível completar o cadastro")
            return response.json() 
        })
        .then(function(data){ 
            alert("Usuário cadastrado com sucesso" + data)
            console.log(data)
        })
        .catch(function(error) {
            console.log('There has been a problem with your fetch operation: ' + error.message);
            alert(error.message);
        });
    }

0 个答案:

没有答案