【问题标题】:Why res.redirect is not redirecting the page in nodejs? [duplicate]为什么 res.redirect 不重定向 nodejs 中的页面? [复制]
【发布时间】:2020-08-29 03:06:55
【问题描述】:

res.redirect('/taskswriter') 不工作意味着不指向我不知道为什么的页面?

Express.js 代码:

router.route('/update-sources').get(function(req, res) {

  User.updateMany({"Addtasks.commonID":req.query.commonIDs},
     {$set: {"Addtasks.$.status" :req.query.selectedValue }},
    function (error, success) {
          if (error) {
              console.log(error);
          } else {
            User.findOne({tag:"Client","Addtasks.commonID":req.query.commonIDs},
            function (error, data) {
                  if (error) {
                      console.log(error);
                  } else {
                    data.Addtasks.forEach(element => {
                      if(element.commonID == req.query.commonIDs)
                      {
                        var mailOptions2 = {
                          from: 'grumpytext@gmail.com',
                          to: data.email,
                          subject: 'Status changed for the article '+ element.topic,
                          text: 'Writer has changed the status to '+ req.query.selectedValue
                        };

                        transporter.sendMail(mailOptions2, function (error, info) {
                          if (error) {
                            return console.log(error);
                          }
                         else {
                           **res.redirect('/taskswriter');**
                         console.log("Mail sent successfully");
                         console.log("Redirected to taskwriter");
                        }
                      });
}
}); //Mail sent

                    }
            });

            }
    });
  })

AJAX 调用:

$.ajax({
  type: "GET",
  url: "/update-sources",
  dataType: "json",
  data: {
    selectedValue: text,
    commonIDs: id
  },
  contentType: "application/json",
  success: function(datas){
            console.log(datas);
          alert("Working!!");
      }
});

单击上述 AJAX 下方选择菜单中的选项之一已被调用。一切正常,只有最后一件事是任务编写器没有被重定向,尽管我已经在同一页面中定义了它的路由。

Hanlebar 选择菜单:

<select name="sourcesSelect" id="{{this.commonID}}" class="custom-select sources" placeholder="{{this.status}}">
      <option value="0">In Progress</option>
      <option value="1">Done</option>
      <option value="2">Rejected</option>
    </select>

【问题讨论】:

  • ajax 中的重定向不会重定向页面,您必须在 JS 中这样做
  • AJAX成功回调中的express路由成功调用后,如何将响应返回给ajax调用?

标签: node.js ajax redirect handlebars.js dropdown


【解决方案1】:

在get route中更新你的else部分,一旦邮件发送成功,而不是使用res.redirect('/taskswriter');使用res.send("SUCCESS");

然后在您的 ajax 调用中删除 dataType 选项,因为如果您将提供 datatype: json 那么 ajax 将期望来自服务器的 json 响应。

试试这个 ajax 调用

$.ajax({
type: 'GET',
url: '/update-sources',
contentType: 'application/json',
data: JSON.stringify({
    "selectedValue": text,
    "commonIDs": id
}),
success: (response) => {
    response = response.trim();
    if(response === "SUCCESS"){
        window.location.href = "http:localhost:3000/taskswriter"; //give location where you want to redirect
    }
},
error: (error) => {
    console.log("ERROR:");
    console.log(error);
    alert(error.responseText);
}});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多