Method not Allowed error on Ajax Post request (500 error) Laravel Framework

991 Views Asked by At

I am working on a Post Ajax request Function. Where the function takes some data and send it through an enabled CSRF_token post Request to a controller and then after evaluations on controller a message sent back to the view. but it seems i miss a small thing in my code.

My controller

 public function PostMessage(Request $request){

    $message=$request->someData;  //getting data from request variable 
    return response()->json($message);
}

My jquery Ajax request function

 $('.SendAjaxPostRequest').on('click', function() {
       var value=$('.MessageHolder').val();

        $.ajax({
            method: 'POST',
            url:'{{route('SVCate')}}', //SVCate is my route to the controller
            dataType: 'JSON',
            data: {_token:token,'someData':value,} 
            // #token gets it's value from a local view javaScrip Variable
        })
            .done(function (data) {
                console.log(data);
            })
    }); 

My route function

    Route::post('SendMessage','NessageController@PostMessage')->name('SVCate');
1

There are 1 best solutions below

1
On

Check your apostrophes in the url. You are ending the string and beginning the string around SVCate change it to url:"{{route('SVCate')}}" to make sure that SVCate stays a string and does not break your string.