Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Submit form and call Ajax on <a> click

I want to add something to database using Ajax. I have a link which submits the form and then Ajax call should work, but it's not. I use this same Ajax call on different page, but in that form I'm using simple button with type submit. But on this page, I want to submit with ...

This is the form

{!! Form::open(['id' => 'ajax-form', 'style' => 'float:right']) !!}
  <input type="hidden" name = "idUser" id="idUser" value="{{Auth::user()->id}}">
  <input type="hidden" name = "idCampaign" id="idCampaign" value="{{$campaign->id}}">
  <a class="fa fa-bookmark fa-2x" onclick="document.getElementById('ajax-form').submit();" aria-hidden="true" href="javascript:{}" style="color:#fd8809"></a>
{!! Form::close() !!}

This is the Ajax:

$("#ajax-form").submit(function(event) {
    event.preventDefault();
    var form = $(this);
    $.ajax({
        type: "post",
        url: "{{ url('addFavorites') }}",
        dataType: "json",
        data: form.serialize(),
        success: function(data){
            if(data.status == 'failedd'){
              swal("Error!", "You have already added this campaign to favorites! If you want to remove it, go to your Favorites list page", "error")
            }
            else{
            swal("Success!", "You added the campaign "+ data.idCampaign + " to favorites!", "success")
          }
        },
        error: function(data){
            swal("Error!", "error")
        },
        complete: function (data) {
     }
    });
});

When I click on this link, it redirects me to another page which throws: MethodNotAllowedHttpException.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Change your ajax url to this:

url: '/addFavorites',

and your route to this:

Route::post('/addFavorites', 'SearchController@addFavorites');

Just to make sure you point at the same url

EDIT:

make sure that you have one route with get and the same route with post. see bellow:

Route::get('/addFavorites', 'SearchController@loadFavorites'); <-to load the page

Route::post('/addFavorites', 'SearchController@submitFavorites'); <-to submit the data

  1. Change this: $("#ajax-form").submit(function(event) {

    To this: $('#yourid').click(function(){

  2. Remove the onclick in the 'a' element and add the id='yourid' with the name you want.

over 4 years ago · Santiago Trujillo Report

0

MethodNotAllowedHttpException usually comes when you haven't defined route or you have defined route for get method and you are using the route for post method. please check your routes.php

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!