Laravel“Symfony\Component\HttpKernel\Exception\HttpException”错误

问题描述 投票:0回答:4

在我的 Laravel、Vue JS 应用程序中,将应用程序传输到我们的服务器后,我收到此类错误。

Symfony\Component\HttpKernel\Exception\HttpException

我在here看到了同样的问题,但没有答案。我希望这次有人可以针对此类错误提供答案或解决方案。

注:

  1. 仅当我尝试使用其他设备从服务器访问应用程序时,才会发生此错误。
  2. 错误发生不定期。如果它不首先显示,有时会在 2 个或更多 ajax 请求后显示。
  3. 我正在使用 vForm 来处理我的 ajax 请求。
  4. 它在我的电脑/本地主机上运行良好。
  5. 当我尝试访问服务器内的应用程序时,它也运行良好。我的意思是,我尝试在我们的服务器内使用浏览器,它工作没有问题。

PS.

  • 我检查了我的 Request Header 并且
    X-CSRF-TOKEN
    就在那里。
  • 在我的请求有效负载
    _token
    也在那里。

以下是我的代码:

VueJS 方法:

UpdateDepartmentInfo(){

            this.departmentToUpdate._token = this.$root.csrf;

            this.requestUpdateDepartmentOnProgress = true;

            this.departmentToUpdate.put('department/'+this.departmentToUpdate.id)
                .then((response) => {              
                    this.requestUpdateDepartmentOnProgress = false;      
                    this.GetDepartmentList();
                    swal(
                        'Department Info. Saved!',
                        'Department information has been successfully updated to database!',
                        'success'
                    );
                })
                .catch((error) => {
                    this.requestUpdateDepartmentOnProgress = false;
                    if (error.response.status == 401) {
                        alert('User session has expired. Please login again.');
                        location.replace("/login");
                    }
                });
        }

Laravel 控制器:

public function update(Request $request, $id)
    {

        // Check if the department object from model if it exists
        try{
            $department = Department::findOrFail($id);
        }catch(\Exception $e){
            return response(['error' => 'The department you want to edit can\'t be found in the database!' ], 400);
        }

        // check if it was just the status was sent 
        if($request['newStat'] != null){
            $department->status  = $request['newStat'];
        }else{

            $this->validate($request,[
                'name'      => 'required|string|max:191',
                'code'      => 'required|string|max:10',
            ]);

            $department->name           = $request['name'];
            $department->code           = $request['code'];
            $department->description    = $request['description'];
            $department->status         = $request['status'];       
        }

        $department->save();
        return ['message' => 'ok'];


    }

提前谢谢您!

php ajax laravel vue.js
4个回答
0
投票

在您的 .env 文件中进行如下更改

APP_URL=http://your_server_url

或者你必须在你的

index.php
文件中放薄的东西

header("Access-Control-Allow-Origin: http://localhost");

0
投票

检查组件中的 resources/js/components 文件夹(如果有)

http://127.0.0.1:8000/fetchusers

更改为

 fetch('fetchusers',{
            method:'POST',
            headers:{
                'Content-Type':'application/json',
                X_CSRF_TOKEN:('meta[name="csrf-token"]').content,
                'Accept':'application/json'
            }
        })

可能会有帮助。


0
投票

通常是因为csrf_token,你可以查看这个文档希望它也能解决你的问题。

https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token

要做的事:

<meta name="csrf-token" content="{{ csrf_token() }}">

    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});```
 

-1
投票

检查您的

storage/log/laravel.log
文件是否存在。他们也有写/读的权限

这可以通过运行轻松修复:

sudo chmod -R 777 storage/logs
© www.soinside.com 2019 - 2024. All rights reserved.