【问题标题】:Laravel "Symfony\Component\HttpKernel\Exception\HttpException" errorLaravel“Symfony\Component\HttpKernel\Exception\HttpException”错误
【发布时间】:2019-03-01 22:30:02
【问题描述】:

在我的 Laravel、Vue JS 应用程序中,我在将应用程序转移到我们的服务器后遇到了这种类型的错误。

Symfony\Component\HttpKernel\Exception\HttpException

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

注意:

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

附言。

  • 我检查了我的请求标头X-CSRF-TOKEN 在那里。
  • 在我的Request Payload中也有_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'];


    }

提前谢谢你!

【问题讨论】:

  • 你可以添加堆栈跟踪吗?
  • 你的.envAPP_URL中有什么
  • @ShaielndraGupta - APP_URL=http://localhost
  • @cbaconnier - 我在laravel.log 中找不到错误或 stracktrace,但在此处的响应中是:(等等,我不知道如何附加屏幕截图)
  • @cbaconnier - 我什至无法在此处粘贴跟踪响应,因为它太长了。

标签: php ajax laravel vue.js


【解决方案1】:

这通常是因为 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')
    }
});```
 

【讨论】:

    【解决方案2】:

    如果您有
    http://127.0.0.1:8000/fetchusers

    ,请检查组件中的 resources/js/components 文件夹

    改成

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

    这可能会有所帮助。

    【讨论】:

      【解决方案3】:

      在你的 .env 文件中进行这样的更改

      APP_URL=http://your_server_url
      

      或者你必须在你的index.php文件中放薄

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

      【讨论】:

      • 尝试了这两个建议,但仍然出现错误。如果我使用服务器浏览器它工作正常,我似乎无法理解为什么我会收到错误原因。如果我尝试公开访问它,我只会收到错误消息。不是因为延迟吗?
      • 更新了答案,如果不行请检查并告诉我
      • 已经将header("Access-Control-Allow-Origin: http://localhost"); 放在public\index.php 的最顶部,但不幸的是错误仍然存​​在。 :(
      【解决方案4】:

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

      这可以通过运行很容易地解决:

      sudo chmod -R 777 storage/logs
      

      【讨论】:

      • 不要建议chmod 777!这不可能是一个解决方案,永远!
      • 这里没有使用linux服务器。他们正在使用 Windows 服务器操作系统。顺便说一句,我检查了我的laravel.log,权限设置为读/写。
      猜你喜欢
      • 2015-03-14
      • 2018-07-21
      • 2019-10-17
      • 2018-09-25
      • 2021-04-28
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      相关资源
      最近更新 更多