简体   繁体   English

Node.Js承诺重定向不起作用

[英]Node.Js promise redirect doesn't work

I have a settings page that you can insert and delete some filters. 我有一个设置页面,您可以插入和删除一些过滤器。 For delete request I used this: 对于删除请求,我使用了这个:

$('#delete-filter').click(function (e) {
        var filtername = $('#filter-list').val();
        var filterCount = $('#filter-list option').length;
            var retVal = confirm("Are you sure to delete this filter?");
            if( retVal == true ){
                $.ajax({
                    url: "/settings?filtername=" + filtername,
                    method: 'DELETE',
                    dataType: "json",
                    success: function (result) {

                    }
                });

           }
           else{
              return false;
           }

    });

And here is my route for this page: 以下是此页面的路线:

router.delete('/settings', ensureAuthenticated, function (req, res, next) {
    var promise = user.deleteFilter(req.session.user_id, req.query.filtername);
    var promise2 = promise.then(function (data) {
        req.session.selected_filter = data.selected;
        res.redirect('/settings');
    }, function (error) {
        console.log(error);
    })
})

Essentially I want to redirect to page to settings, so the page reloads with the new data. 基本上我想重定向到页面到设置,所以页面重新加载新数据。 But in promise chain I can't use any response functions. 但在承诺链中我不能使用任何响应函数。 Am I using the redirect wrong? 我使用重定向错了吗? or I can't send a response in promise chain ? 或者我不能在承诺链中发送回复?

You've misidentified the problem. 你错误地认定了这个问题。

When you issue an HTTP redirect, you say "The thing you were looking for? Get it from here instead." 当您发出HTTP重定向时,您会说“您正在寻找的东西?从此处获取它”。

This is not the same as "The browser should display this URL as a new page". 这与“浏览器应将此URL显示为新页面”不同。

The HTTP redirect is followed, the settings page is delivered to the browser, then the browser makes it available as the result in your success function. 遵循HTTP重定向,将设置页面传送到浏览器,然后浏览器将其作为成功函数的result (You then completely ignore it as you haven't put anything in that function). (然后你完全忽略它,因为你没有在该函数中放置任何东西)。

If you want the browser to load a new page, then you need to deliver the URL as data (not as a redirect) and then assign that value to location.href . 如果您希望浏览器加载新页面,则需要将URL作为数据 (而不是重定向)传递,然后将该值分配给location.href

The settings page probably shouldn't be determined dynamically, so you can probably just hard code the URL into the success function. 设置页面可能不应该动态确定,因此您可以将URL硬编码到成功函数中。

Hard coding it would make more sense, since you shouldn't send a redirect in response to a DELETE request : 硬编码会更有意义,因为您不应该发送重定向来响应DELETE请求

If a DELETE method is successfully applied, the origin server SHOULD send a 202 (Accepted) status code if the action will likely succeed but has not yet been enacted, a 204 (No Content) status code if the action has been enacted and no further information is to be supplied, or a 200 (OK) status code if the action has been enacted and the response message includes a representation describing the status. 如果成功应用了DELETE方法,则原始服务器应该发送202(已接受)状态代码(如果操作可能成功但尚未颁布),如果操作已经颁布则为204(无内容)状态代码,不再进一步如果已经执行了操作,则提供信息或200(OK)状态代码,并且响应消息包括描述状态的表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM