Question

[Solved] Symfony Component HttpKernel Exception MethodNotAllowedHttpException No message Laravel 5.8 and Ajax

I’m trying to store multiple emails in a prospect system. I’m building the system with Laravel 5.8. I’m trying to put the emails using the AJAX request. And not return in a page using redirect, but keep in the same modal.

This is for a system that in the past, can only store one email. But a user says that need to store more than one email to the client. Now i’m trying to create a functionality that in a prospect, you can add multiple emails and when the user will send a proposal, will show all emails stored in that prospect.

I’m need to use the AJAX request to do it. Now I try to use the method “POST” to proceed with my functionality, i put it in my route, my view and my AJAX request.

This is my route

Route::POST('prospect/emails/save','[email protected]')->name('prospectEmails.store');

This is my view

 <form id="emailModalProspect" method="POST">
          @csrf
          
          <input hidden name="prospect_id" id="prospect_id" type="text">

          <div class="form-group mb-3">
            <label class="form-control-label" for="prospect-email-name">{{ __('Nome') }}</label>
              <div class="input-group input-group-alternative">
                  <div class="input-group-prepend">
                      <span class="input-group-text"><i class="ni ni-email-83"></i></span>
                  </div>
                  <input class="form-control" id="prospect-email-name" name="name" placeholder="Put the email owner" type="text">

              </div>
          </div>

          <div class="form-group mb-3">
              <label class="form-control-label" for="prospect-email-email">{{ __('Email') }}</label>
              <div class="input-group input-group-alternative">
                  <div class="input-group-prepend">
                      <span class="input-group-text"><i class="ni ni-email-83"></i></span>
                  </div>
                  <input class="form-control" id="prospect-email-email" name="email" placeholder="Put the email" type="email">
              </div>
          </div>

          <div class="text-center">
              <button type="submit" id="save-email" class="btn btn-primary my-4 store-email">Store</button>
          </div>
    </form>

This is my controller

public function store(Request $request){ 
    $prospect_emails = ProspectEmails::where(['prospect_id'=>$request->prospect_id])->get();
     
    ProspectEmails::create(array_merge($request->all(), ['company_id' => Auth::User()->company_id], ['prospect_id'=>$request->prospect_id], ['tag'=>false]));

    $p_email = ProspectEmails::where('prospect_id',$request->prospect_id)->get()->count();
    
    $update_at = CarbonCarbon::now();

    Prospect::where('id', $request->prospect_id)->update(['updated_at' => $update_at, 'prospect_emails'=> $p_email]);
   
    return response()->json();
}

And this is my Ajax request

$('.open-email-modal').on('click', function(e) {
        e.preventDefault();


        let p = JSON.parse($(this).attr('data-entity')); //the id of the prospect that i want to insert the emails
        
        let modal = $('#emailModal');
        let form = $('#emailModalProspect');

        $('#prospect-email-name').val(p.name);
        $('#prospect_id').val(p.id).change();

        form.submit(function(e){
          if(p.id) {
            $.ajax({
                url: "{{route('prospectEmails.store')}}",
                type: "POST",
                data : form.serialize() ,
                dataType: "json",
                success:function(data) {
                  if(data){ 
                    console.log(data); // here, in console, show a empty array like it "[]".
                  }
                }
              });
            }
        });
        modal.modal({show : true});    
  });

Now always I’m trying to register a new email, show this message:

Symfony Component HttpKernel Exception MethodNotAllowedHttpException

No message

In console show this error.

POST http://127.0.0.1:8000/prospect 405 (Method Not Allowed)

The strange that store the data in my database, but show it. What happens to show this message?

I change to PUT method. At first it works, but show the token and the things that i store in URL. It’s not what i want.

Solution #1:

Your route is defined as prospect/emails/save, but you’re POSTing to prospect, which apparently doesn’t exist as a POST route.

Post to the correct route and it should work fine. Adding an action to your <form> should do the trick:

<form action="{{ route('prospectEmails.store') }}" ...>
Respondent: ceejayoz

Solution #2:

please use

Route::any('prospect/emails/save','[email protected]')->name('prospectEmails.store');

or

Route::post('prospect/emails/save','[email protected]')->name('prospectEmails.store');

instead of

Route::POST('prospect/emails/save','[email protected]')->name('prospectEmails.store')

please use routes like this

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Respondent: monu0002

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy