I'm using Laravel and I want to delete a record, for this I'm using an XMLHttpRequest, the problem is that, since I'm using the routes through 'Resource' (Route::resource('/url',Controller::Class);)
Well, to access the 'Destroy' method, it asks me to send the parameters with the DELETE method from the view, and I don't know how to do it with the XMLHttpRequest requests, remembering Ajax, I tried to do this, but it didn't work
const http = new XMLHttpRequest();
const url = "/Medicos/" + id ;
const type = 'DELETE';
http.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
let Medico = this.responseText;
console.log(Medico);
}
};
http.open("GET", url);
http.send();
Also try changing the
http.open('DELETE', url);
But it throws me this error
DELETE http://moon.project/Medicos/2 419 (unknown status)
The request has to be
POST
and you will have to send a field_method
with valueDELETE
. In addition, as in all requestsPOST
, you also have to send a field_token
with the onecsrf_token
from your application.You can put that in an object
FormData
for example:If you're not making the call in a blade template, you can add the token to a tag
<meta>
within<head>
your application's main blade template tag:And
token
you can get it from there:Ref XMLHttpRequest: Methods .
Ref Laravel: Routing .