处理来自 Angular POST 的快速重定向 [英] Handle an express redirect from Angular POST

查看:25
本文介绍了处理来自 Angular POST 的快速重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Expressjs 作为 API,我使用 angular 来点击该 POST.我想回应快递发送的重定向.我的 Angular POST 成功返回我打算重定向到的页面的 HTML,但我的 DOM 上没有任何反应.我可以看到我的重定向在我的网络流量中起作用,并且下面的 console.log 数据包含重定向页面的 DOM.

I'm using Expressjs as an API and I'm using angular to hit that POST. I would like to respond to the redirect that express is sending. Success from my Angular POST returns a HTML of the page I intend to redirect to but nothing happens on my DOM. I can see that my redirect is working in my network traffic, and that console.log data, below contains the DOM of the redirected page.

如何刷新 DOM,以反映此成功的 POST,或处理重定向"?

How can I refresh the DOM, to reflect this successful POST, or handle the "redirect"?

角度代码:

   $http({method: 'POST', url: '/login', data:FormData}).
      success(function(data, status, headers, config) {
      console.log(data)
    }).
      error(function(data, status, headers, config) {
    });

    $scope.userName = '';

Expressjs API:

Expressjs API:

    app.post('/login', function(req, res){

        var name = req.param('name', null);  // second parameter is default
    var password = req.param('password', "changeme")

    // Lots Authenticationcode
    // returns succesfful login
    res.redirect('http://localhost:3000/'); 

  }); // end app.post

console.log(data)(来自 Angular POST 成功)

console.log(data) (from Angular POST success)

returns the HTML of the page I intended to redirect to

推荐答案

AngularJS 旨在作为客户端框架与(主要是)RESTfull API 结合使用.您的登录 API 不应该返回 HTML,它应该返回重定向指令.因此,在您的情况下,您应该在 $http 成功回调中简单地调用 $location.url('/'),这将使用 Angular 路由器重定向"到 '/'(根 URL).

AngularJS is meant to work as a client-side framework coupled with (mostly) RESTfull APIs. Your login API isn't supposed to return a HTML, it is supposed to return the instruction to redirect. So in your case you should simply call $location.url('/') in your $http success callback, which would use the Angular router to "redirect" to '/' (the root URL).

这篇关于处理来自 Angular POST 的快速重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆