【问题标题】:in Laravel I GOT AN ERROR The POST method is not supported for this route. Supported methods: GET, HEAD在 Laravel 中我遇到了错误这条路线不支持 POST 方法。支持的方法:GET、HEAD
【发布时间】:2021-12-31 11:53:45
【问题描述】:

我正在尝试当客户想要上传图像或徽标时,我希望此图像通过 span 元素显示在他面前,以确保他将正确的图像上传到系统,但是当我按下上传时我得到这条路线不支持 POST 方法。支持的方法:GET、HEAD。错误

这是我在 card_view_blade 中的代码

 <div class="form-group row">
                            <div class="col-md-8">
                                <form method="post" id="upload-image-form" enctype="multipart/form-data">
                                @csrf
                                <div class="input-group"  data-type="image">
                                    <input type="file" name="file" class="form-control" id="image-input">
                                    
                                    <button type="submit" class="btn btn-success">Upload</button>
                                </div>
                                </form>
                            </div>
                            
                            <div class="col-md-4"> 
                                <div class="alert" id="message" style="display: none"></div>
                                <span id="uploaded_image"></span>
                            </div>
                        </div>

这里是js代码

@section('script')

    <script type="text/javascript">
     $(function(){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#upload-image-form').submit(function(e) {
            e.preventDefault();
            let formData = new FormData(this);
            $('#message').hide().html('');

            $.ajax({
                type:'POST',
                url: `/upload-images`,
                data: formData,
                dataType:'JSON',
                contentType: false,
                cache: false,
                processData: false,
                success: (data) => {
                    console.log("success-",data);
                    if (data) {
                        this.reset();
                        $('#message').show().html(data.message);
                        $('#message').addClass(data.class_name);
                        $('#uploaded_image').html(data.uploaded_image);
                    }
                    setTimeout(function(){
                        $('#message').hide().html('');
                    }, 3000);
                },
                error: function(data){
                    console.log("error-",data);
                    // $('#image-input-error').text(data.responseJSON.errors.file);
                    $('#message').show().html('Something went wrong');
                    $('#message').addClass('danger');
                    $('#uploaded_image').html('');
                    setTimeout(function(){
                        $('#message').hide().html('');
                    }, 3000);
                }
            });
        });
    })
    </script>
@endsection

路线代码

Route::post('/upload-images', 'CheckoutController@storeImage' )->name('images.store');

【问题讨论】:

  • 该错误页面显示您确实在使用 POST,因此您的刀片和 js 代码无关紧要。在控制台中,尝试php artisan route:list - 确保您的帖子端点已列出,并且上面没有任何可能与它发生冲突的东西
  • 感谢您的帮助

标签: javascript php laravel


【解决方案1】:

代码似乎没有问题,也许路由被缓存了。尝试先清除它们,然后使用以下命令查看问题是否已解决:

php artisan route:clear

【讨论】:

  • 欢迎您!这应该是评论!
  • 同意丹尼尔,我是新用户,所以我现在不能做任何 cmets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 2020-04-19
  • 2020-01-13
  • 2023-02-05
  • 2021-05-08
  • 2020-01-22
  • 2020-04-25
相关资源
最近更新 更多