[英]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: 在看到问题并提出答案后,请注意以下几点:
Route::get("api/test", function(){});
不需要Route::get("api/test", function(){});
can be accessed by /api/test
. 可以通过/api/test
访问。 /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.