【问题标题】:Laravel 6 - Route post not foundLaravel 6 - 找不到路由帖子
【发布时间】:2020-06-14 10:22:46
【问题描述】:

我正在尝试在 NuxtJS 中实现 Register User。但是,我遇到了如下问题

vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php

In My api Route

 Route::post('register', 'V1\Auth\LoginController@register');

我正在尝试将其更改为get method 并通过127.0.0.1:8000/api/register 访问它,它的工作非常好。

我还是不知道,为什么我得到这个POST http://127.0.0.1:3000/api/register 404 (Not Found)

In My Nuxtjs

           async registerUser() {
                try {
                    await this.$axios.post(`api/register`, this.auth);
                    await this.$auth.login({
                        data: {
                            email: this.auth.email,
                            password: this.auth.password,
                        }
                    })
                    .then(res => {
                        console.log(res);
                        this.loading = false
                        // this.$router.push(`/auth/login`);
                    })
                    .catch(err => {
                        this.loading = false
                        this.$refs.form.validate(err.response.data.errors)
                        console.log(err.response);
                    })
                } catch(e) {
                    // statements
                    console.log(e);
                }

In My Controller

    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required|between:6, 25',
        ]);

        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        return response()->json(['registered' => true ]);
    }

感谢您的所有帮助。
谢谢。

【问题讨论】:

    标签: php vue.js nuxt.js


    【解决方案1】:

    你在axios中使用的是3000端口http://127.0.0.1:3000/api/register,但是laravel使用的是8000http://127.0.0.1:8000/api/register

    【讨论】:

      【解决方案2】:

      您必须在 NuxtJS APP 中建立 API 基础 URL。

      原因是您的 NuxtJS 应用程序默认设置了它自己的基本 URL 但是你必须为 laravel API 配置后端 URL。

      例子,

      试试这个

      await this.$axios.post(`http://127.0.0.1:8000/api/register`, this.auth);
      

      【讨论】:

        猜你喜欢
        • 2016-03-29
        • 1970-01-01
        • 2014-05-12
        • 1970-01-01
        • 2016-11-15
        • 2020-04-16
        • 2020-03-26
        • 2016-04-24
        • 2014-12-29
        相关资源
        最近更新 更多