Laravel - MethodNotAllowedHttpException in RouteCollection.php line 251












0















My question is about upload photo with 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:



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:



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




I can't figure it out. How can I solve this?










share|improve this question

























  • javascript post url is /api/media/create but your route is just media/create I guess that should be the problem ?

    – sking
    Jan 19 at 11:48











  • Route defined in the routes/api.php Within this group, the /api URI prefix is automatically applied. @sking

    – M. Özdemir
    Jan 19 at 11:56













  • do you get the error as a post error in the console or do you get the error view of laravel?

    – François Lanzeray
    Jan 19 at 11:59











  • GET localhost:8000/api/media/… 405 (Method Not Allowed) I'm getting this error. @FrançoisLanzeray

    – M. Özdemir
    Jan 19 at 12:01











  • So you get these MethodNotAllowed Exceptions when you defined a post route but you do not send a post request to this route. And as you are getting a GET error, you are sending a get. I'd guess everythings fine on the laravel part, but there seems to be a problem in you're html/js, I'll have a look into it...

    – François Lanzeray
    Jan 19 at 12:09
















0















My question is about upload photo with 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:



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:



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




I can't figure it out. How can I solve this?










share|improve this question

























  • javascript post url is /api/media/create but your route is just media/create I guess that should be the problem ?

    – sking
    Jan 19 at 11:48











  • Route defined in the routes/api.php Within this group, the /api URI prefix is automatically applied. @sking

    – M. Özdemir
    Jan 19 at 11:56













  • do you get the error as a post error in the console or do you get the error view of laravel?

    – François Lanzeray
    Jan 19 at 11:59











  • GET localhost:8000/api/media/… 405 (Method Not Allowed) I'm getting this error. @FrançoisLanzeray

    – M. Özdemir
    Jan 19 at 12:01











  • So you get these MethodNotAllowed Exceptions when you defined a post route but you do not send a post request to this route. And as you are getting a GET error, you are sending a get. I'd guess everythings fine on the laravel part, but there seems to be a problem in you're html/js, I'll have a look into it...

    – François Lanzeray
    Jan 19 at 12:09














0












0








0








My question is about upload photo with 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:



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:



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




I can't figure it out. How can I solve this?










share|improve this question
















My question is about upload photo with 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:



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:



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




I can't figure it out. How can I solve this?







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 11:57







M. Özdemir

















asked Jan 19 at 11:35









M. ÖzdemirM. Özdemir

34




34













  • javascript post url is /api/media/create but your route is just media/create I guess that should be the problem ?

    – sking
    Jan 19 at 11:48











  • Route defined in the routes/api.php Within this group, the /api URI prefix is automatically applied. @sking

    – M. Özdemir
    Jan 19 at 11:56













  • do you get the error as a post error in the console or do you get the error view of laravel?

    – François Lanzeray
    Jan 19 at 11:59











  • GET localhost:8000/api/media/… 405 (Method Not Allowed) I'm getting this error. @FrançoisLanzeray

    – M. Özdemir
    Jan 19 at 12:01











  • So you get these MethodNotAllowed Exceptions when you defined a post route but you do not send a post request to this route. And as you are getting a GET error, you are sending a get. I'd guess everythings fine on the laravel part, but there seems to be a problem in you're html/js, I'll have a look into it...

    – François Lanzeray
    Jan 19 at 12:09



















  • javascript post url is /api/media/create but your route is just media/create I guess that should be the problem ?

    – sking
    Jan 19 at 11:48











  • Route defined in the routes/api.php Within this group, the /api URI prefix is automatically applied. @sking

    – M. Özdemir
    Jan 19 at 11:56













  • do you get the error as a post error in the console or do you get the error view of laravel?

    – François Lanzeray
    Jan 19 at 11:59











  • GET localhost:8000/api/media/… 405 (Method Not Allowed) I'm getting this error. @FrançoisLanzeray

    – M. Özdemir
    Jan 19 at 12:01











  • So you get these MethodNotAllowed Exceptions when you defined a post route but you do not send a post request to this route. And as you are getting a GET error, you are sending a get. I'd guess everythings fine on the laravel part, but there seems to be a problem in you're html/js, I'll have a look into it...

    – François Lanzeray
    Jan 19 at 12:09

















javascript post url is /api/media/create but your route is just media/create I guess that should be the problem ?

– sking
Jan 19 at 11:48





javascript post url is /api/media/create but your route is just media/create I guess that should be the problem ?

– sking
Jan 19 at 11:48













Route defined in the routes/api.php Within this group, the /api URI prefix is automatically applied. @sking

– M. Özdemir
Jan 19 at 11:56







Route defined in the routes/api.php Within this group, the /api URI prefix is automatically applied. @sking

– M. Özdemir
Jan 19 at 11:56















do you get the error as a post error in the console or do you get the error view of laravel?

– François Lanzeray
Jan 19 at 11:59





do you get the error as a post error in the console or do you get the error view of laravel?

– François Lanzeray
Jan 19 at 11:59













GET localhost:8000/api/media/… 405 (Method Not Allowed) I'm getting this error. @FrançoisLanzeray

– M. Özdemir
Jan 19 at 12:01





GET localhost:8000/api/media/… 405 (Method Not Allowed) I'm getting this error. @FrançoisLanzeray

– M. Özdemir
Jan 19 at 12:01













So you get these MethodNotAllowed Exceptions when you defined a post route but you do not send a post request to this route. And as you are getting a GET error, you are sending a get. I'd guess everythings fine on the laravel part, but there seems to be a problem in you're html/js, I'll have a look into it...

– François Lanzeray
Jan 19 at 12:09





So you get these MethodNotAllowed Exceptions when you defined a post route but you do not send a post request to this route. And as you are getting a GET error, you are sending a get. I'd guess everythings fine on the laravel part, but there seems to be a problem in you're html/js, I'll have a look into it...

– François Lanzeray
Jan 19 at 12:09












3 Answers
3






active

oldest

votes


















0














Could you try to put that route in web.php ? then call it from there without including the /api/ in the link






share|improve this answer
























  • I get same error.

    – M. Özdemir
    Jan 19 at 13:17



















0














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. 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)

  2. API Routes defined in api.php do always auto prepend api/ so theres no need to specifically type that in the route files

  3. Leading / in routes are not necessary Route::get("api/test", function(){}); can be accessed by /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. So be careful :)






share|improve this answer

































    0














    It was about old jQuery version I change jQuery version and problem solved. Thanks for helping.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266656%2flaravel-methodnotallowedhttpexception-in-routecollection-php-line-251%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Could you try to put that route in web.php ? then call it from there without including the /api/ in the link






      share|improve this answer
























      • I get same error.

        – M. Özdemir
        Jan 19 at 13:17
















      0














      Could you try to put that route in web.php ? then call it from there without including the /api/ in the link






      share|improve this answer
























      • I get same error.

        – M. Özdemir
        Jan 19 at 13:17














      0












      0








      0







      Could you try to put that route in web.php ? then call it from there without including the /api/ in the link






      share|improve this answer













      Could you try to put that route in web.php ? then call it from there without including the /api/ in the link







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 19 at 13:05









      KneegrowsKneegrows

      54




      54













      • I get same error.

        – M. Özdemir
        Jan 19 at 13:17



















      • I get same error.

        – M. Özdemir
        Jan 19 at 13:17

















      I get same error.

      – M. Özdemir
      Jan 19 at 13:17





      I get same error.

      – M. Özdemir
      Jan 19 at 13:17













      0














      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. 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)

      2. API Routes defined in api.php do always auto prepend api/ so theres no need to specifically type that in the route files

      3. Leading / in routes are not necessary Route::get("api/test", function(){}); can be accessed by /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. So be careful :)






      share|improve this answer






























        0














        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. 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)

        2. API Routes defined in api.php do always auto prepend api/ so theres no need to specifically type that in the route files

        3. Leading / in routes are not necessary Route::get("api/test", function(){}); can be accessed by /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. So be careful :)






        share|improve this answer




























          0












          0








          0







          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. 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)

          2. API Routes defined in api.php do always auto prepend api/ so theres no need to specifically type that in the route files

          3. Leading / in routes are not necessary Route::get("api/test", function(){}); can be accessed by /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. So be careful :)






          share|improve this answer















          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. 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)

          2. API Routes defined in api.php do always auto prepend api/ so theres no need to specifically type that in the route files

          3. Leading / in routes are not necessary Route::get("api/test", function(){}); can be accessed by /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. So be careful :)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 13:34

























          answered Jan 19 at 13:29









          François LanzerayFrançois Lanzeray

          838




          838























              0














              It was about old jQuery version I change jQuery version and problem solved. Thanks for helping.






              share|improve this answer




























                0














                It was about old jQuery version I change jQuery version and problem solved. Thanks for helping.






                share|improve this answer


























                  0












                  0








                  0







                  It was about old jQuery version I change jQuery version and problem solved. Thanks for helping.






                  share|improve this answer













                  It was about old jQuery version I change jQuery version and problem solved. Thanks for helping.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 21 at 10:22









                  M. ÖzdemirM. Özdemir

                  34




                  34






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266656%2flaravel-methodnotallowedhttpexception-in-routecollection-php-line-251%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Liquibase includeAll doesn't find base path

                      How to use setInterval in EJS file?

                      Petrus Granier-Deferre