Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS-jquerydelete方法_Javascript_Jquery_Node.js - Fatal编程技术网

Javascript NodeJS-jquerydelete方法

Javascript NodeJS-jquerydelete方法,javascript,jquery,node.js,Javascript,Jquery,Node.js,我试图在单击按钮时发送删除请求,但我不确定是否设置了jQuery,尽管使用了用于删除方法的路由,我仍然收到GET请求错误无法GET/app/DELETE/1。我的jQuery是否在正确的轨道上?如何防止此GET请求 路线: appRoutes.route('app/delete/:testId') .delete(function(req, res){ models.Test.destroy({ where: {

我试图在单击按钮时发送删除请求,但我不确定是否设置了jQuery,尽管使用了用于删除方法的路由,我仍然收到GET请求错误
无法GET/app/DELETE/1
。我的jQuery是否在正确的轨道上?如何防止此GET请求

路线:

appRoutes.route('app/delete/:testId')

    .delete(function(req, res){
        models.Test.destroy({
            where: {
                userId: req.user.user_id,
                testId: req.params.testId           
            }
        }).then(function(){
            res.redirect('/app');
        });
    });
链接:


jQuery:

<script type="text/javascript">
        $(document).ready(function() {
            $('#test-delete-link').click(function(){
                $.ajax({
                    type: 'DELETE',
                    url: '/app/delete/{{test.testId}}'
                });
            });
        });
    </script>

$(文档).ready(函数(){
$(“#测试删除链接”)。单击(函数(){
$.ajax({
键入:“删除”,
url:“/app/delete/{{test.testId}”
});
});
});

问题在于,当单击链接时,它使用
GET
。单击链接时,会发生两件事

  • 单击
    事件被触发,jQuery发送
    删除
    请求
  • 浏览器向同一路径发送
    GET
    请求,因为它是一个链接
要解决此问题,请将链接的
href
属性设置为
“javascript:void(0);”
。这使得单击链接除了在
单击事件中指定的内容外,什么也不做


作为旁注,我将请求的路径从
/app/delete/:testId
更改为
/app/:testId
。现在的方式是多余的,正如您所说的
DELETE/app/DELETE/1
。更为RESTful的方法是
DELETE/app/1

问题是,当单击链接时,它使用
GET
。单击链接时,会发生两件事

  • 单击
    事件被触发,jQuery发送
    删除
    请求
  • 浏览器向同一路径发送
    GET
    请求,因为它是一个链接
要解决此问题,请将链接的
href
属性设置为
“javascript:void(0);”
。这使得单击链接除了在
单击事件中指定的内容外,什么也不做


作为旁注,我将请求的路径从
/app/delete/:testId
更改为
/app/:testId
。现在的方式是多余的,正如您所说的
DELETE/app/DELETE/1
。更安静的方式是
DELETE/app/1

Hey@voidbige,谢谢您的回复。你的回答与DELET请求有关,但我很好奇为什么我的重定向从未被触发。我应该使用ajax方法吗?@cphill您应该使用ajax方法。重定向不起作用的原因是它重定向的是ajax请求,而不是浏览器所在的页面。不要在服务器上调用
redirect
,而是在收到服务器的响应后在客户端上重定向。嘿@voidbige,谢谢您的响应。你的回答与DELET请求有关,但我很好奇为什么我的重定向从未被触发。我应该使用ajax方法吗?@cphill您应该使用ajax方法。重定向不起作用的原因是它重定向的是ajax请求,而不是浏览器所在的页面。不要在服务器上调用
redirect
,而是在收到服务器的响应后在客户端上重定向。
<script type="text/javascript">
        $(document).ready(function() {
            $('#test-delete-link').click(function(){
                $.ajax({
                    type: 'DELETE',
                    url: '/app/delete/{{test.testId}}'
                });
            });
        });
    </script>