TokenMismatchException when deleting a row in db
Ad
I'm using DropZone.js and laravel. When i try to delete a record with an AJAX request i get a response saying:
Whoops, looks like something went wrong. 1/1 TokenMismatchException in VerifyCsrfToken.php
var classElements = document.querySelectorAll("tr.ui-selected td.filename");
for(var x = 0;x < classElements.length;x++){
var result;
result = classElements[x].innerHTML;
var csrf = $('input[name=_token]').val();
$.ajax({
async: true,
type: "DELETE",
method: 'POST',
url: '../public/deletefile',
data: { filename: result, "_token": "{{ csrf_token() }}" },
success: function(response) {
$('#results').html(response);
}
});
This is the model:
public function deleteUserFiles(){
$userid = Auth::id();
$result = $_POST['result'];
$deletedRows = App\Models\File::where('filename', $result)->where('userid', $userid)->delete();
}
}
And the route:
Route::post('deletefile', '[email protected]');
What could be the problem?
Ad
Answer
Ad
I think the problem here is that you use csrf_token()
in loop. I think your JS code should look like this:
var classElements = document.querySelectorAll("tr.ui-selected td.filename");
var csrf = $('input[name=_token]').val();
for(var x = 0;x < classElements.length;x++){
var result;
result = classElements[x].innerHTML;
$.ajax({
async: true,
type: "DELETE",
method: 'POST',
url: '../public/deletefile',
data: { filename: result, "_token": csrf },
success: function(response) {
$('#results').html(response);
}
});
Ad
source: stackoverflow.com
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad