简体   繁体   English

Laravel ajax catch 500内部服务器错误

[英]Laravel ajax catch 500 Internal server error

I am using Laravel with ajax, and in my controller I have a try - catch block: 我正在使用带有ajax的Laravel,并且在我的控制器中我有一个try-catch块:

try {
        something...
    } catch (\Exception $e) {
        return response()->json([
            'status' => 'error',
            'message'=> 'error message'
        ]);
    }

I am using this to show the error message in a div on the page. 我使用它来在页面上的div中显示错误消息。

This is working, but not if the error is a 500 - internal server error (example: tokenmismatchexception). 这是有效的,但如果错误是500 - 内部服务器错误(例如:tokenmismatchexception)则不行。

Then the error is not caught by the catch-block, and essentially the user is not notified of any error (other than in the console). 然后catch-block没有捕获错误,实际上用户不会收到任何错误的通知(除了在控制台中)。

Is there a way I can catch such errors, and display an error in the same div as I usually do? 有没有办法可以捕捉到这样的错误,并在同一个div中显示错误,就像我通常那样?

That is because you might not have included the error section. 那是因为您可能没有包含错误部分。 Make your jQuery Ajax code like: 使你的jQuery Ajax代码如下:

    $.ajax({
        url     : '/yoururl',
        method    : 'POST',
        data    :
        {
            name:"test"
        },
        success   : function(response)
        {
           //USE THIS SECTION WHEN ITS SUCCESS
        },
        error : function(e)
        {
          //SHOW ERROR TO USER HERE THIS SECTION IS CALLED IN CASE OF ERRORS TO SEE THE OBJECT OF the error use console.log(e);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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