Skip to content

Instantly share code, notes, and snippets.

@roNn23
Forked from JeffreyWay/laravel.js
Last active July 16, 2019 11:41
Show Gist options
  • Save roNn23/a86f31ecaf0c6e0a7d65 to your computer and use it in GitHub Desktop.
Save roNn23/a86f31ecaf0c6e0a7d65 to your computer and use it in GitHub Desktop.
Updated delete-script of Jeffrey Way to work with Laravel 5. And optimized the implementation for the CSRF token. #laravel
/*
<a href="posts/2" data-method="delete"> <---- We want to send an HTTP DELETE request
- Or, request confirmation in the process -
<a href="posts/2" data-method="delete" data-confirm="Are you sure?">
Add this to your view:
<script>
window.csrfToken = '<?php echo csrf_token(); ?>';
</script>
<script src="/js/deleteHandler.js"></script>
*/
(function() {
var laravel = {
initialize: function() {
this.registerEvents();
},
registerEvents: function() {
$('body').on('click', 'a[data-method]', this.handleMethod);
},
handleMethod: function(e) {
var link = $(this);
var httpMethod = link.data('method').toUpperCase();
var form;
// If the data-method attribute is not PUT or DELETE,
// then we don't know what to do. Just ignore.
if ( $.inArray(httpMethod, ['PUT', 'DELETE']) === - 1 ) {
return;
}
// Allow user to optionally provide data-confirm="Are you sure?"
if ( link.data('confirm') ) {
if ( ! laravel.verifyConfirm(link) ) {
return false;
}
}
form = laravel.createForm(link);
form.submit();
e.preventDefault();
},
verifyConfirm: function(link) {
return confirm(link.data('confirm'));
},
createForm: function(link) {
var form =
$('<form>', {
'method': 'POST',
'action': link.attr('href')
});
var token =
$('<input>', {
'name': '_token',
'type': 'hidden',
'value': window.csrfToken
});
var hiddenInput =
$('<input>', {
'name': '_method',
'type': 'hidden',
'value': link.data('method')
});
return form.append(token, hiddenInput)
.appendTo('body');
}
};
laravel.initialize();
})();
@dylanglockler
Copy link

Love this script and I had it working but now for some reason it comes back with an error listed below. I followed the instructions carefully - linked to the deletehandler.js and I'm echoing the csrf_token. I get no javaScript errors. Anyone have an idea how to fix this?

I'm on Laravel 5.

1/1
MethodNotAllowedHttpException in RouteCollection.php line 207:
in RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 720
at Router->findRoute(object(Request)) in Router.php line 643
at Router->dispatchToRoute(object(Request)) in Router.php line 619
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 55

@sp-99
Copy link

sp-99 commented Jul 29, 2015

My link just submits a get request to the href still

Copy link

ghost commented Nov 11, 2015

So no sign of a resolution there. I'll try emailing Github.

@furqan99
Copy link


still getting token mismatch error

@DanJFletcher
Copy link

DanJFletcher commented Feb 20, 2017

This works fine for me - thank you!

Guys, make sure you don't forget:

  <script>
		window.csrfToken = '<?php echo csrf_token(); ?>';
  </script>

The quotes wrapping the CSRF token are important. Otherwise this script will fail, hence why you end up sending a GET request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment