简体   繁体   English

为什么我的表单不会向数据库提交用户信息?

[英]Why won't my form submit user info to the database?

I'd like to know what's wrong in my code in terms of why it's not sending username, email and password to my database via an Axios POST request.我想知道我的代码有什么问题,为什么它没有通过 Axios POST 请求向我的数据库发送用户名、email 和密码。 My Laravel endpoint works fine because I verified via PostMan.我的 Laravel 端点工作正常,因为我通过 PostMan 进行了验证。 I'm just struggling with the front end portion of this.我只是在这个前端部分苦苦挣扎。 What am I doing wrong and how can I fix this?我做错了什么,我该如何解决?

There error I'm getting on my browser says: Cannot POST /register我的浏览器出现错误提示: Cannot POST /register

import React, {Component} from 'react';
import axios from "axios";

class Register extends Component {
    constructor(props) {
        super(props);
        this.state = {
            username: "",
            email: "",
            password: ""
        };
        this.userNameHandler = this.userNameHandler.bind(this);
        this.emailHandler = this.emailHandler.bind(this);
        this.passwordHandler = this.passwordHandler.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    userNameHandler(e) {
        this.setState({
            username: e.target.value,
        });

        console.log(this.state.username);
    }

    emailHandler(e) {
        this.setState({
            username: e.target.value,
        });

        console.log(this.state.email);
    }

    passwordHandler(e) {
        this.setState({
            username: e.target.value,
        });

        console.log(this.state.password);
    }

    handleSubmit(e) {
        const user = {
            name: this.state.name,
            email: this.state.email,
            password: this.state.password
        }

        this.setState({
            username: e.target.value
        });

        axios.post('http://127.0.0.1:8000/api/auth/signup', user)
            .then(response => {
                console.log(response);
                console.log(response.data);
            });
    }

    render() {
        return (
            <div className="container">
                <div id="login-row" className="row justify-content-center align-items-center">
                    <div id="login-column" className="col-md-6">
                        <div id="login-box" className="col-md-12">
                            <form id="login-form" className="form" method="post" onSubmit={this.handleSubmit}>
                                <div className="form-group">
                                    <label htmlFor="username" className="text-info">Username:</label><br/>
                                    <input type="text" name="username" id="username" className="form-control" onChange={this.userNameHandler}/>
                                </div>
                                <div className="form-group">
                                    <label htmlFor="username" className="text-info">Email:</label><br/>
                                    <input type="text" name="username" id="username" className="form-control" onChange={this.emailHandler}/>
                                </div>
                                <div className="form-group">
                                    <label htmlFor="password" className="text-info">Password:</label><br/>
                                    <input type="text" name="password" id="password" className="form-control" onChange={this.passwordHandler}/>
                                </div>
                                <div className="form-group">
                                    <input type="submit" name="submit" className="btn btn-info btn-md"
                                           value="Submit"/>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default Register;

You haven't called e.preventDefault() , so you get a regular form submission to the URL specified in the action attribute.您还没有调用e.preventDefault() ,因此您会收到一个常规表单提交给action属性中指定的 URL 。 You don't have one of those, but the default URL to submit to is the current URL .您没有其中之一,但要提交的默认 URL 是当前 URL

The Ajax request gets cancelled as the browser navigates away from the current page to load a new one (which is the server saying it doesn't support POST requests to that URL). Ajax 请求在浏览器离开当前页面以加载新页面时被取消(服务器表示它不支持对该 URL 的 POST 请求)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM