admin管理员组文章数量:1332865
So, supposed i have this link in my ejs file :
<a href="/user/12">Delete</a>
And in my route file, i have delete code like following :
router.delete( '/user/:id', function ( req, res ) {
// delete operation stuff
});
So my question is, how i can override GET
request from link into DELETE
method to ensure my router.delete
route able to handle it. Right now, its only detect the request as a GET
. I'm using this Method Override module to handle it, But seems like all of examples were using form element, not the anchor way. Anyone?
So, supposed i have this link in my ejs file :
<a href="/user/12">Delete</a>
And in my route file, i have delete code like following :
router.delete( '/user/:id', function ( req, res ) {
// delete operation stuff
});
So my question is, how i can override GET
request from link into DELETE
method to ensure my router.delete
route able to handle it. Right now, its only detect the request as a GET
. I'm using this Method Override module to handle it, But seems like all of examples were using form element, not the anchor way. Anyone?
1 Answer
Reset to default 9Anyway, right now here is the solutions i used to override GET
request by using middleware
before application request was made, so far for the link i change the href
to look like this :
<a href="/user/12?_method=DELETE" >Delete</a>
And in route :
router.use( function( req, res, next ) {
// this middleware will call for each requested
// and we checked for the requested query properties
// if _method was existed
// then we know, clients need to call DELETE request instead
if ( req.query._method == 'DELETE' ) {
// change the original METHOD
// into DELETE method
req.method = 'DELETE';
// and set requested url to /user/12
req.url = req.path;
}
next();
});
Finally, the requested path will match this route :
router.delete( '/user/:id', function ( req, res ) {
// delete operation stuff
});
Anyone who encountered this problems may try it out and if anyone who facing this problems and able to solved it with great solutions, please let me know. Happy codding!
本文标签: javascriptOverride method GET to DELETE in nodeJS using anchor tagStack Overflow
版权声明:本文标题:javascript - Override method GET to DELETE in nodeJS using anchor tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292731a2448113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论