Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

javascript - Handle an express redirect from Angular POST

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...