【问题标题】:Laravel 405 method not allowed in live hosting实时托管中不允许使用 Laravel 405 方法
【发布时间】:2020-09-02 11:18:46
【问题描述】:

由于很多天我都在尝试在服务器中上传文件,因此在实时服务器中出现了 405 个方法不允许错误: 这是我的看法: HTML & javascript

@extends('layouts.app')
@section('content')
form id="uploaddiamond" class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
@csrf
     <div class="col-md-6">
        <div class="block">
            <div class="panel-body">
              <div class="form-group">
                    <label class="col-md-3 control-label">Upload Diamond <span class="required">*</span></label>
                    <div class="col-md-9">
                        <input required="" type="file" name="result_file" id="result_file" />
                    </div>
                </div>

                <div class="btn-group pull-right">
                    <button class="btn btn-primary" type="submit">Submit</button>
                </div>
            </div>
        </div>
    </div>
</form>
@endsection()
@section('javascript')
<script>
$("#uploaddiamond").on("submit",function(e) {
    e.preventDefault();
    console.log('tst');
        $.ajaxSetup({
            headers: {
                'X-CSRF-Token': $('meta[name=_token]').attr('content')
            }
        });
        var file_data = $('#result_file').prop('files')[0];
        var form_data = new FormData();
        form_data.append('file', file_data);
        $.ajax({
            url: "{{ route('diamond') }}", // point to server-side PHP script
            data: form_data,
            type: 'POST',
            contentType: false, // The content type used when sending data to the server.
            cache: false, // To unable request pages to be cached
            processData: false,
            success: function(data) {
                console.log(data);
            }
        });
});
</script>
@endsection()

This is my web route:
Route::get('/imageview','ImageController@index')->name('getimage');
Route::post('/postDiamond','ImageController@postDiamond')->name('diamond');
这是我的控制器:
 public function index(){
        return view('Image/imgupload');
    }


    public function postDiamond(Request $request){
    dd($request->file('file'));
        $supplier_name = $request->supplier_name;
        $extension = $request->file('file');
        $extension = $request->file('file')->getClientOriginalExtension(); // getting excel extension
        $dir = 'assets/files/';
        $filename = uniqid().'_'.time().'_'.date('Ymd').'.'.$extension;
        $request->file('file')->move($dir, $filename);
    }

我不知道为什么这段代码不起作用,因为这段代码在 localhost 中有效,但在 Linux 主机中无效: 有人可以帮忙吗我在版本上做错了什么 服务器当前 PHP 版本:7.3.17 laravel PHP 版本:7.1.10

这是我的服务器错误图片,请检查: enter image description here

enter image description here

【问题讨论】:

  • 您是否也在元标记中包含了 csrf 令牌?全球范围内?
  • 是的,先生,我已经命名为 _token
  • 尝试更改此顺序并检查url: "{{ route('diamond') }}", data: form_data,type: 'POST', TO type: 'POST', url: "{{ route('diamond') }}", data: form_data
  • 你能显示路由图吗?默认情况下在 routes/web.php 但是如果你有 laravel 7,需要 php version >= 7.2.5 但我怀疑一个错误的方法,比如错误说
  • 没有任何作用,先生

标签: php linux laravel file-upload web-hosting


【解决方案1】:

我刚刚在实时服务器上测试了这段代码,它工作正常。如果此代码仍然对您不起作用,那么您需要在服务器端检查某些文件的权限,例如 web.php 等...

$(document).on('click','#submit_button', function(e){ //#submit_button id on submit button
  e.preventDefault();
  var formData = new FormData(this.form);

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

$.ajax({
    method: 'POST',
    url: '{{ route("diamond") }}',
    cache: false,
    contentType: false,
    processData: false,
    data: formData,

    success: function(data){
        console.log(data);
    },
    error: function(data){
        console.log(data);
    }
  });
});

请务必从您的 &lt;form&gt; 标记中删除 @csrf。 在控制器中只需 dd(request()-&gt;all()); 看看你会得到什么

【讨论】:

  • 清除服务器端的缓存 php artisan route:clear , php artisan cache:clear
  • 试过了,先生,您的代码适用于本地主机,但不在服务器中仍然无法运行您的代码是完美的,但是,我对我的服务器有疑问,我不是服务器专家,请告诉我有一些解决方案
  • web.php 权限为 755,Route::get('/clear', function() { Artisan::call('cache:clear'); Artisan::call('config: clear'); Artisan::call('config:cache'); Artisan::call('view:clear'); Artisan::call('route:clear'); return "Cleared!"; });并在服务器中使用此路由清除
  • 如果仍然无法正常工作,请给这个链接几分钟并阅读它。它可能会对您有所帮助,尤其是关注本站点中的服务器端配置。 airbrake.io/blog/http-errors/405-method-not-allowed
  • DirectoryIndex index.php &lt;IfModule mod_rewrite.c&gt; &lt;IfModule mod_negotiation.c&gt; Options -MultiViews &lt;/IfModule&gt; RewriteEngine On RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ RewriteRule ^(.*) - [E=BASE:%1] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] RewriteCond %{REQUEST_FILENAME} -f RewriteRule .? - [L] RewriteRule .? %{ENV:BASE}/index.php [L] &lt;/IfModule&gt; 先生,这是我的 .htaccess conf 请验证它是否正确?仍在阅读您提供的帖子
猜你喜欢
  • 2020-07-25
  • 2017-11-28
  • 2017-05-31
  • 1970-01-01
  • 2013-03-15
  • 2018-09-09
  • 2015-04-11
  • 2019-03-25
  • 2017-12-19
相关资源
最近更新 更多