DELETE method using href - Nodejs

Asked

Viewed 336 times

1

I am loading some information from the bank and one of the columns will be a link that will delete an infomation.

By being in a table, the link href is like this:

href="/delete/ID(vem do template engine)?_method=DELETE

He should go to my route that’s like this:

router.delete('/delete/:_id', "Resultado do meu controller"));

The configuration of my app.js to inherit the PUT and DELETE options looks like this:

app.use(methodOverride('X­HTTP­Method'));
app.use(methodOverride('X­HTTP­Method­Override'));
app.use(methodOverride('X­Method­Override'));
app.use(methodOverride('_method'));

That way, there is something else I can do so that when clicking the delete link is redirected to the route router.delete?

2 answers

0


I ended up doing it that way:

                <a href="javascript:void(0)" onClick="ExcluirRegistro('/stormtroppers/',{{stormtropper.id}})">Delete</a>

javascript:

function ExcluirRegistro(url, user_id) {
if (confirm('Deseja excluir o registro?')) {
    $.ajax({
        url: url + user_id,
        type: 'DELETE',
        success: function (res) {
            window.location.reload();
            return false;
        },
        error: function (xhr, status, error) {
            console.log(xhr.responseText);
            alert("Error deleting");
            return false;
        }
    });
}

}

Still... thanks for the info!

0

When the href is used, the browser uses the method GET to request. In the module documentation method-override it is written that by default the only HTTP method it overwrites is the POST. In order for the module to do what you need, you will need to pass the methods you want to overwrite to the module. Change the following line:

app.use(methodOverride('_method'));

for:

app.use(methodOverride('_method', {methods: ['GET']}));

If you want to keep the POST just add the string 'POST' in the module’s permitted method array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.