简体   繁体   中英

405 Method Not Allowed for POST and PUT in laravel application

I'm trying to drop and resize an event in fullcalendar in laravel, but when I move the event, I have 405 Method Not Allowed error message!..

My web.php file:

 Route::get('calendar', 'FullCalendarController@index'); Route::get('/load-events', 'EventController@loadEvents')->name('routeLoadEvents'); Route::put('/events-update', 'EventController@update')->name('routeEventUpdate');

My blade.php file:

 <div id='calendar' data-route-load-events="{{route('routeLoadEvents')}}" data-route-events-update="{{route('routeEventUpdate')}}"></div>

My .js file:

 $(function (){ $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); }); function sendEvent(route, data_){ $.ajax({ url: route, data: data_, method: 'POST', dataType: 'json', success: function(json){ if(json){ location.reload(); } } }); } function routeEvents(route){ return document.getElementById('calendar').dataset[route]; } eventDrop: function(element){ let start = moment( element.event.start ).format("YYYY-MM-DD HH:mm:ss"); let end = moment( element.event.end ).format("YYYY-MM-DD HH:mm:ss"); let newEvent = { _method: 'PUT', id: element.event.id, start: start, end: end }; sendEvent(routeEvents('routeEventUpdate'), newEvent); },

So there are really only two types of request that Laravel really understands:

POST & GET

A PUT request is a POST request with a method of PUT .

What laravel is looking for is a _method parameter in the request telling it what type of request this is. in your case _method: PUT .


Try this:

$.ajax({
    url: route,
    data: data_,
    method: 'PUT',
    dataType: 'json',
    success: function(json){
        if(json){
            location.reload();
        }
    }
});

You put your "put" method in your payload-data, this wont be recocnized by the AJAX Function. Try adding the method as an Argument

function sendEvent(route, data_, sendMethod){
...
method: sendMethod,
...

And call i via

sendEvent(routeEvents('routeEventUpdate'), newEvent, 'PUT');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM