响应代码200而不是500 laravel with angular js

时间:2018-02-08 07:25:40

标签: php angularjs laravel http server-response

我正面临着我的项目问题,我将从Angularjs前端调用API到laravel后端api。

在我的 authenticationController.php uthenticate方法我要返回

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

web.php

中使用此路线

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

当我返回此信息时,我得到res ponse代码200而不是500并且响应为字符串而不是json

code 200 instead of 500

string instead of 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] => 
)

0 个答案:

没有答案