控制器的Axios调用生成500(内部服务器错误)

时间:2019-10-03 11:55:56

标签: laravel axios vuex

我正在尝试在控制器中实现注册功能,但是当我执行tryRegister操作时,浏览器会记录500(内部服务器错误)。因此,我从register函数中删除了所有内容,只是尝试返回一个简单的字符串作为响应。而且奇怪的是它仍然会产生该错误。因此,由于登录已经可以进行,所以我尝试在登录函数中执行消息响应,并使用tryLogin操作对其进行调用,但是疯狂的事情是登录函数响应得很好并且记录了“登录”?我究竟做错了什么?它与request参数有关系吗?因为那是两个功能之间的唯一区别。

此外,我还是后端开发的新手,我真的在尝试掌握这一点。

Vuex动作

import axios from "axios";

export default {
    tryRegister(context, credentials) {
        context.commit("login");
        return new Promise((resolve, reject) => {
            axios
                .post("/api/auth/register", credentials)
                .then(response => {
                    console.log(response.data);
                    context.commit("loginSucces", response.data);
                    resolve(response.data);
                })
                .catch(error => {
                    console.log(error.response);
                    reject(error);
                });
        });
    },
    tryLogin(context, credentials) {
        context.commit("login");
        return new Promise((resolve, reject) => {
            axios
                .post("/api/auth/login", credentials)
                .then(response => {
                    console.log(response.data);
                    context.commit("loginSucces", response.data);
                    resolve(response.data);
                })
                .catch(error => {
                    context.commit("loginFailed", error);
                    reject(error);
                });
        });
    },
    tryLogout(context) {
        context.commit("logout");
    }
};

路线

<?php

Route::group([
    'prefix' => 'auth'

], function () {
    Route::post('register', 'AuthController@register');
    Route::post('login', 'AuthController@login');
    Route::post('logout', 'AuthController@logout');
    Route::post('refresh', 'AuthController@refresh');
    Route::post('me', 'AuthController@me');
});

授权控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class AuthController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login', 'register']]);
    }

    public function register(Request $request)
    {
        // $user = User::create([
        //     'email'    => $request->email,
        //     'password' => $request->password,
        // ]);

        // $token = auth('api')->login($user);

        // return $this->respondWithToken($token);
        return "register";
    }

    /**
     * Get a JWT via given credentials.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login()
    {
        // $credentials = request(['email', 'password']);

        // if (!$token = auth('api')->attempt($credentials)) {
        //     return response()->json(['error' => 'Unauthorized'], 401);
        // }

        // return $this->respondWithToken($token);
        return "login";
    }

    /**
     * Get the authenticated User.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return response()->json(auth('api')->user());
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        auth('api')->logout();

        return response()->json(['message' => 'Successfully logged out']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this->respondWithToken(auth('api')->refresh());
    }

    /**
     * Get the token array structure.
     *
     * @param  string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'user' => $this->guard()->user(),
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 60
        ]);
    }

    public function guard()
    {
        return Auth::Guard('api');
    }
}

1 个答案:

答案 0 :(得分:1)

您缺少导入this.firstUserFullName = result.entities[0].fullname;的信息。

Request class