Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message Laravel 5.8 and Ajax

你说的曾经没有我的故事 提交于 2020-02-07 05:46:24

问题


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','ProspectEmailsController@store')->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 = Carbon\Carbon::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.


回答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') }}" ...>



回答2:


please use

Route::any('prospect/emails/save','ProspectEmailsController@store')->name('prospectEmails.store');

or

Route::post('prospect/emails/save','ProspectEmailsController@store')->name('prospectEmails.store');

instead of

Route::POST('prospect/emails/save','ProspectEmailsController@store')->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);


来源:https://stackoverflow.com/questions/57697010/symfony-component-httpkernel-exception-methodnotallowedhttpexception-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!