1

I have an application with angularjs 1.6 and Java 8. I want to send POST data to another application and go to the url that this application determine.

The domain is that my application send data of a citizen that want to take a turn of a service. The other application, take this data and show a view with a form with all fields autocompleted with the data i had send. I have to do like this, because the user asks for it.

I have tried several ways to do this, for example with xmlHttpRequest but when I sent the post, the user page is not redirected (despite receiving status 302).

A possible solution that I have tried

$http({
                method: "POST",
                headers: { 
                    ...some headers...
                },
                data : someData,
                url: someExternalUrl
            })
            .then(function(response, headers) {
                //catch the location header of response
                var externalUrl = headers("Location");
                $window.location.href = externalUrl;
            }, 
            function(response) {
                //if fail
            });

Another:

            var url = someUrl;
            var params = JSON.stringify(jsonObject);
            http.open('POST', url, true);

            //Send the proper header information along with the request
            //http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            //Some other headers

            http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4 && http.status == 200) {
                    alert(http.responseText);
                }
            }
            http.send(params);

How can i do it?

Thanks!

Geronimo
  • 461
  • 5
  • 23
  • Can you add some piece of your code written in angularjs or javascript? – FarukT Nov 22 '18 at 15:41
  • It is not clear if you are implementing both server and client code. Nevertheless you should understand that status codes are intended to be a semantic indication for the user agent who is makign the request. Check this [post](https://stackoverflow.com/questions/36638150/best-approach-to-redirect-an-url-using-rest) that can help you. – Caio Saldanha Nov 22 '18 at 15:49
  • Thanks for answering @FarukT, i edit the post. I am implementing only client code. – Geronimo Nov 22 '18 at 17:12
  • @CaioSaldanha, i am implementing cliente code only. – Geronimo Nov 22 '18 at 17:12
  • @PranavVR you're wrong, I do not want to do a post and then redirect. I would like the post redirect the page like when you send a form. – Geronimo Nov 22 '18 at 17:17
  • The `.then` method has an erroneous handler function. The `headers` function is exposed as a property of the response object, not as a second argument. – georgeawg Nov 23 '18 at 01:34
  • @Geronimo if you write a console.log(externalUrl) it writes a link that begins with 'http://www.yoururl.com'? – FarukT Nov 23 '18 at 08:50

1 Answers1

-1

302 will not redirect your browser if you're using xmlHttpRequest (or fetch). You need to retrieve the data and do a window.location = 'newUrlHere'; whenever your post call is done.

You can do it several ways, here are two of them:

  • Your POST method returns a 302, you look at the headers, get the url and set the window location;
  • Your POST method returns an object or string in the body that has the new location you need to go. Get that and do window.location = newLocation; and you're good to go.
Sergio Moura
  • 4,888
  • 1
  • 21
  • 38
  • `window.location.replace` [would be better](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – George Nov 22 '18 at 15:42
  • @George -- that depends. If the user uses `replace`, the back button will not bring the user back to the original location. It's good that @Geronimo knows both methods exist, tough, so he can make the proper decision for his use case. – Sergio Moura Nov 22 '18 at 15:44
  • @SergioMoura -- Thanks for answering. The problem is that by doing that, it will not be the same request. When I do the post, the request is sent to the other application and then when I redirect (once I got the response of the request) I would make a new request so it would not work – Geronimo Nov 22 '18 at 17:01
  • @SergioMoura That is not the way browsers work when receiving a 302 response from an XHR POST request. Browsers transparently follow 302 redirects. Read [Returning redirect as response to XHR request](https://stackoverflow.com/questions/282429/returning-redirect-as-response-to-xhr-request). – georgeawg Nov 23 '18 at 01:27