简体   繁体   English

Laravel-RouteCollection.php第251行中的MethodNotAllowedHttpException

[英]Laravel - MethodNotAllowedHttpException in RouteCollection.php line 251

My question is about upload photo with Ajax. 我的问题是关于使用Ajax上传照片。

This is my blade: 这是我的刀片:

<section class="panel">
    <header class="panel-heading">
        Medya Ekle
    </header>
    <div class="panel-body">
        <form class="form-horizontal tasi-form" id="upload_form" method="post" enctype="multipart/form-data">
            {{csrf_field()}}
            <div class="form-group">
                <label class="col-sm-2 control-label">Medya Başlığı *</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control mediaTitleTxt" name="mediaTitleTxt" autocomplete="off" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Medya *</label>
                <div class="col-sm-10">
                    <input type="file" class="form-control mediaInput" name="mediaInput" autocomplete="off" required>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-12">
                    <button class="btn btn-success pull-right addMediaBtn">Ekle</button>
                </div>
            </div>
        </form>
    </div>
</section>

<section class="panel tasks-widget">
    <header class="panel-heading">
        Medyalar
    </header>
    <div class="panel-body">

    </div>
</section>

<!--main content end-->

This is my JS code: 这是我的JS代码:

let form = $("#upload_form");
form.on("submit", function (e) {
    e.preventDefault();
    $.ajax({
        url:"/api/media/create",
        method:"POST",
        data:new FormData(this),
        dataType:'JSON',
        contentType: false,
        cache: false,
        processData: false,
        success:function(data)
        {
            console.log(data);
        }
    });
});

This is my routes/api.php: 这是我的路线/api.php:

Route::post("media/create", "api@createMedia");

This is my controller: 这是我的控制器:

public function createMedia(Request $request){
    //TODO Upload image
    return [$request];
}

When I click submit button I'm getting this error: 当我单击提交按钮时,出现此错误:

MethodNotAllowedHttpException in RouteCollection.php line 251 RouteCollection.php第251行中的MethodNotAllowedHttpException

I can't figure it out. 我不知道。 How can I solve this? 我该如何解决?

Could you try to put that route in web.php ? 您可以尝试将该路由放入web.php吗? then call it from there without including the /api/ in the link 然后从那里调用它,而不必在链接中包含/ api /

For anyone wondering: problem is that he used GET in his code at some point, but he fixed it by now and it's working. 对于任何想知道的人:问题是他在某个时候在代码中使用了GET,但是他现在已经将其修复,并且可以正常工作。 Please see the comments to his question for further information. 请参阅对他的问题的评论以获取更多信息。 Some general notes after I've seen the questions and proposed answers: 在看到问题并提出答案后,请注意以下几点:

  1. General information about Laravel Routes can be obtained from the docs: https://laravel.com/docs/5.7/routing (make ure you use the one version that matches your laravel version) 有关Laravel路线的一般信息可以从以下文档中获取: https ://laravel.com/docs/5.7/routing(确保您使用与laravel版本相匹配的一个版本)
  2. API Routes defined in api.php do always auto prepend api/ so theres no need to specifically type that in the route files api.php中定义的API路由始终自动在api /之前添加前缀,因此无需在路由文件中专门键入
  3. Leading / in routes are not necessary Route::get("api/test", function(){}); 不需要Route::get("api/test", function(){}); can be accessed by /api/test . 可以通过/api/test访问。
  4. However, trying to access it by using /api/test/ results in an MethodNotAllowedException as Laravel assumes that after the / something follows as a get parameter. 但是,尝试通过使用/api/test/来访问它会导致MethodNotAllowedException,因为Laravel假定/之后是get参数。 So be careful :) 所以要小心:)

It was about old jQuery version I change jQuery version and problem solved. 这是关于旧的jQuery版本,我更改了jQuery版本并解决了问题。 Thanks for helping. 感谢您的帮助。

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

相关问题 RouteCollection.php第251行中的MethodNotAllowedHttpException - MethodNotAllowedHttpException in RouteCollection.php line 251 RouteCollection.php第251行中的MethodNotAllowedHttpException: - MethodNotAllowedHttpException in RouteCollection.php line 251: RouteCollection.php中的MethodNotAllowedHttpException(第251行) - MethodNotAllowedHttpException in RouteCollection.php (line 251) 在RouteCollection.php第251行中的DELETE MethodNotAllowedHttpException: - on DELETE MethodNotAllowedHttpException in RouteCollection.php line 251: RouteCollection.php 第 219 行中的 Laravel 5 MethodNotAllowedHttpException - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219 Laravel 5:RouteCollection.php第219行中的MethodNotAllowedHttpException - Laravel 5: MethodNotAllowedHttpException in RouteCollection.php line 219 RouteCollection.php第218行中的Laravel MethodNotAllowedHttpException: - Laravel MethodNotAllowedHttpException in RouteCollection.php line 218: MethodNotAllowedHttpException在laravel 5中的RouteCollection.php第207行 - MethodNotAllowedHttpException RouteCollection.php line 207 in laravel 5 RouteCollection.php第201行中的Laravel 5 MethodNotAllowedHttpException: - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201: Laravel 5在RouteCollection.php第219行中的MethodNotAllowedHttpException: - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM