最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handle an express redirect from Angular POST - Stack Overflow

programmeradmin1浏览0评论

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.

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

ANGULAR CODE:

   $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:

    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) (from Angular POST success)

returns the HTML of the page I intended to redirect to

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.

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

ANGULAR CODE:

   $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:

    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) (from Angular POST success)

returns the HTML of the page I intended to redirect to
Share Improve this question edited May 18, 2013 at 21:26 JZ. asked May 18, 2013 at 21:18 JZ.JZ. 21.9k33 gold badges119 silver badges193 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

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).

发布评论

评论列表(0)

  1. 暂无评论