简体   繁体   中英

Laravel / axios not returning response json only status code error

I am throwing an exception and handling it in handler.php, but it doesn't return the message: My handler looks like this:

public function render($request, Exception $exception)
{
    if($exception instanceof ModelNotFoundException){
        return response()->json([ 'message' => ''.str_replace('App\\', '', $exception->getModel()).' not found.'], 404);
    }

    if($exception instanceof AuthenticationException){
        return response()->json(['message' => "Unauthorized1"], 401);
    }

    if($exception instanceof InvalidStateException){
        return response()->json(['message' => $exception->getMessage()], 400);
    }
    return parent::render($request, $exception);
}

But changes reflect only if I change the status code not the message. At my frontend I am using axios and when I catch the exception there is no json in it. This is how my message looks in chrome:

POST http://127.0.0.1:8000/api/login 401 (Unauthorized)
remote.js:11 Error: Request failed with status code 401
    at e.exports (spread.js:25)
    at e.exports (spread.js:25)
    at XMLHttpRequest.d.onreadystatechange (spread.js:25)

This is my axios request:

const login = (user) => axios.post(base + 'api/login', user).catch(e => console.log(e));

它实际上来自 axios 请求,我应该这样做

catch(e => console.log(e.response));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM