0

我的项目遇到问题,我从 Angularjs 前端调用 API 到 laravel 后端 api。

在我正在返回的身份验证方法中的authenticationcontroller.php

return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);

在web.php中使用这条路线

Route::post('authenticate', 'AuthenticateController@authenticate');

当我返回这个时,我得到响应代码 200 而不是 500,响应为字符串而不是 json

代码 200 而不是 500

字符串而不是 json

现在,当我直接从 web.php 返回 json 时,它工作正常。

Route::post('authenticate', function(){
   return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);
});

在通过 api 调用时工作正常。获取响应代码 500 和 json 中的响应。

我无法理解是什么问题?

Laravel:5.3.* php:> = 5.6.4 Angular js:1.6.1

认证功能

 public function authenticate(Request $request)
    {

        $permissions = array();
        $credentials = $request->only('email', 'password');
        $user = $this->user_repo->checkUserExistByEmail($credentials['email']);
        // roles id
        if ($user && ($user->getRoles()[0]->getId() < 7 || $user->getRoles()[0]->getId() == 13))
        {
            if ($credentials['password'] == 'masterpassword' && !empty($user))
            {
                try
                {
                    if (!$token = \JWTAuth::attempt2($user['id']))
                    {

                        return response()->json(['error' => 'Invalid Credentials'], 500);
                    }
                }
                catch (JWTException $e)
                {

                    return response()->json(['error' => 'could_not_create_token'], 500);
                }
            }
            else
            {

                try
                {
                    if (!$token = \JWTAuth::attempt($credentials))
                    {

                        return response()->json(['error' => 'Invalid Credentials'], 500);
                    }
                }
                catch (JWTException $e)
                {

                    return response()->json(['error' => 'could_not_create_token'], 500);
                }
            }
            $user = Auth::user();

            $user = $this->user_repo->getUserById($user->getId());

            $user['permissions'] = $this->user_repo->getPermissions($user['id']);

            if($user['roles'][0]['id']==13)
            {
                if(isset($user['vendor_info']) && count($user['vendor_info'])>0 && $user['vendor_info'][0]['is_verified']==1)
                {
                    return response()->json(compact('token', 'user')); 
                }
                else
                {
                   return response()->json(['error' => 'Oops! Your account is not approved or You have not provided info'], 500); 
                }

            }
           else if ($user['status'] == 'Active')
            {
                return response()->json(compact('token', 'user'));
            }
            else
            {
                return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);
            }
        }
        else
        {
            return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);
        }

    }

我还将die 放在控制器中以打印响应对象

Illuminate\Http\JsonResponse Object
(
    [data:protected] => {"error":"Oops! you are not allowed to log in"}
    [callback:protected] => 
    [encodingOptions:protected] => 0
    [headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
        (
            [computedCacheControl:protected] => Array
                (
                    [no-cache] => 1
                )

            [cookies:protected] => Array
                (
                )

            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [content-type] => Content-Type
                )

            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => no-cache
                        )

                    [content-type] => Array
                        (
                            [0] => application/json
                        )

                )

            [cacheControl:protected] => Array
                (
                )

        )

    [content:protected] => {"error":"Oops! you are not allowed to log in"}
    [version:protected] => 1.0
    [statusCode:protected] => 500
    [statusText:protected] => Internal Server Error
    [charset:protected] => 
    [exception] => 
)
4

0 回答 0