AJAX POST request throws "MethodNotAllowedHttpException No message" error right after I submit it

Posted 7 months ago by Gabotronix

Hi, I'm trying to make the basic subscription service, sasly laravel is throwing the following error right after I submit the form via AJAX POST request:

 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

What could be the cause of this?

Route:

Route::post('/subscripcion', '[email protected]')->name('sub');

JQuery code:

    
    
    
    $(".startSubscription").click(function(){
        var form = $(this).closest('form');
        $('.form_error_text').hide();
        $('.form_valid_text').hide();
        console.log('button start subscription clicked');
        
        var emailval=$("input[name='email']").val();
        
        
        $.ajaxSetup({
            
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')}
        
        });
        
        $.ajax({
            
            async: true,
            cache: false,
            url: '{{ route('sub') }}',
            type: 'POST',
            data: 
            {
                
            'email': emailval
            
            },
            dataType: 'JSON',
            
            success: function (data) { 
                $('.form_valid_container').html('<span class="form_valid_text">✓ '+ data.success +'</span>');
                form.trigger("reset");
                console.log(data.success);
            },
            
            error: function (data){
                var errors = data.responseJSON;
                console.log(errors);
                
                $.each(errors , function(){
                    $('.form_error_container').html('<span class="form_error_text">✘ '+ errors.message +'</span>')
                }); 
            }

        }); 
    });
    

});

My view:

<section class="section_sub_mino_maincontainer" style="width:100%; height:200px; display:flex; flex-direction:column;  justify-content:center; align-items:center; ">
    <form class="section_sub_mino_form_container" method="POST" style="width:auto; height:auto; display:flex; flex-direction:column; align-items:center;">
        <span class="section_sub_mino_form_title" style="font-size:24px; color:rgba(0,0,0,0.7); margin:10px 0px; font-weight:600;">Subscribete para recibir nuestras últimas ofertas, noticias y mucho más</span>
        <div class="section_sub_mino_form_newsletter_container"  style="width:400px; height:40px; outline:1px solid rgba(0,0,0,0.3); display:flex; margin:10px 0px;">
            <input class="section_sub_mino_form_newsletter_input" style="width:80%; height:100%; padding:10px;" type="text" name="email" placeholder="Tu correo electrónico">
            <button class="section_sub_mino_form_newsletter_button startSubscription" style="height:100%; padding:0px 15px; font-size:18px; text-transform:uppercase; display:flex;background-color:var(--web_primary_color); color:white;  align-items:center; ">SUBSCRIBIRME</button>
        </div>
        <div class="form_valid_container"></div>
        <div class="form_error_container"></div>
    </form>
</section>

Controller:

<?php

namespace App\Http\Controllers;

use App\Emails;
use Illuminate\Http\Request;
use App\Http\Requests\StoreSubbedMail;

class SubscriptionController extends Controller
{
    
    
    
    
    public function index()
    {
        //
    }

    
    
    
    public function create()
    {
        //
    }

    
    
    
    public function store(StoreSubbedMail $request)
    {
        $email = new Emails();
        $email->subbed_mail = $request->email;
        $email->save();
        
        return response()->json(['success'=>'Te subscribiste correctamente!'], $email);
    }

Form request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreSubbedMail extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'subbed_mail' => 'required|email|unique:emails',
        ];
    }
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.