How do I make a POST + Redirect with Angular?

Asked

Viewed 72 times

0

Hello, I need to post to a page and redirect along with the post to when load the page display the posted data.

The problem is that when I do the post it already brings me the RESPONSE with the loaded data and if I do the redirect I will be doing a GET.

You can post and open the page in the same request?

var data = {
    'nomede' :'Paulo',
    'emailde':'[email protected]',
    'sobrenomede':'Sousa',
    'sapde':'19811',
    'nomepara':'João',
    'emailpara':'[email protected]'
}

$http({
    url: 'http://pagina.com',
    method: "POST",
    params: data
}).then(function (response) {
    console.log(response.data);
    $window.location.href = 'http://pagina.com';
},
function (response) { // optional
    // failed
});

I can do this easily with JQUERY

var form = '';
$.each(data, function (key, value) {
form += '<input type="hidden" name="' + key + '" value="' + value + '">';
});
$('<form action="' + url + '" method="POST">' + form + '</form>').appendTo('body').submit();
  • Paulo, I’m doing a course at Udemy and recently did a post. The code is here: https://github.com/mMarcos208/MeuAppAngularUdemy/blob/master/src/app/orderService.ts Who knows can help you.

  • @Marconi This is Angular 2?

  • Angular 4, this is the course: Building Web Applications with the New Angular 4

  • 1

    I don’t think I’ll just trade the framework for that, but thanks anyway

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.