The POST method is not supported for this route. Supported methods: GET, HEAD. – {Ajax, jQuery, Laravel}


I’m trying to delete an image using a button click. I’m using jQuery ajax to send the post request to the back end and the back end is Laravel.

Here’s my html code

<div class="deal-images-in-new-deal-modal" id="deal-images-after-uploading-section" style="display:flex; justify-content: center; padding:10%; flex-wrap: wrap;"> 
  <div class="new-deal-upload-image">                                 
    <img src="" width="150" height="150">                              
  </div>                             
  <div class="new-deal-upload-icon">                                 
    <button type="submit" class="btn delete_image_button" id="after_image_upload_0" value="151159">                                     
      <i class="trash icon" id="after_image_upload" style="color:red;"></i>                                 
    </button>                             
  </div> 
  <div class="new-deal-upload-image">                                 
    <img src="" width="150" height="150">                              
  </div>                             
  <div class="new-deal-upload-icon">                                 
    <button type="submit" class="btn delete_image_button" id="after_image_upload_1" value="151160">                                     
      <i class="trash icon" id="after_image_upload" style="color:red;"></i>                                
   </button>                             
  </div> 
  <div class="success-message">Test</div>
</div>

Here’s the JS script file

$(document).on('click', '.delete_image_button',function(e) {
    e.preventDefault();
   var image_id = $(this).val();


    $.ajax({
        type: "POST",
        url: '/deals/delete-image/'.image_id,
        dataType: 'JSON',
        success: function(data) {
            console.log("inside success fuction", data);
            html = "<div id='success-message'>The image id is "+data+"</div>";
            $('.success-message').replaceWith(html);
        }
    });
});

Here’s the route in the Laravel Routes file
Routes.php

Route::post('deals/delete-image/{image_id}','[email protected]');

Here’s the code laravel controller file

MainController.php

public function deleteDealImages($image_id) {
        Log::info("inside deleteDealImages function in back end");
        return $image_id;
    } 

I’m getting 405 status code with error response as

{
    "message": "The POST method is not supported for this route. Supported methods: GET, HEAD.",
    "exception": "SymfonyComponentHttpKernelExceptionMethodNotAllowedHttpException",
    "file": "/var/www/vendor_portal/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
    "line": 117
}

I’ve tried many different ways but still keep getting this error. Can someone please help me with this scenario ?

Source: JavaSript – Stack Overflow

September 2, 2021
Category : News
Tags: ajax | javascript | jquery | laravel | php

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.