通过React将数据发布到laravel

时间:2019-10-08 11:44:32

标签: php reactjs laravel api

我试图通过React表单注册用户,将值传递给Laravel中的控制器并插入数据库中。问题是,然后我按了提交按钮,在控制台中我得到了错误:

  

POST('localhost:3001 / api / register')404未找到

有我的api.php文件

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::post('register','Auth\RegisterController@create');

RegisterForm.js

import { addUser } from './RegisterFunctions';

...

onSubmit = event => {
    event.preventDefault();
    addUser(this.state.name, this.state.email, this.state.password).then(() => {
      this.getAll();
    });
    this.setState({
      name: '',
      email: '',
      password: ''
    });
  };
...

和RegisterFunctions.js

import axios from 'axios';


export const addUser = (name, email, password) => {
    return axios
        .post('/api/register',
            {
                name:name,
                email:email,
                password:password
            },
            {
                headers: {'Content-Type': 'application/json'}
            })
        .then(res => {
            console.log(res)
        })
        .catch(err => {
            console.log(err)
        })
};

此外,我在package.json中定义了代理。 我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您正在向错误的端点提出请求 将您的baseUrl更改为

localhost:portNumber // port that your php server is running on

希望有帮助