简体   繁体   English

如何发送发布数据并使用angularjs转到url?

[英]How to send post data and go to url with angularjs?

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. 我有一个使用angularjs 1.6和Java 8的应用程序。我想将POST数据发送到另一个应用程序,然后转到该应用程序确定的URL。

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). 我尝试了几种方法来执行此操作,例如使用xmlHttpRequest,但是当我发送帖子时,用户页面未重定向(尽管接收状态为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! 谢谢!

302 will not redirect your browser if you're using xmlHttpRequest (or fetch). 如果您使用的是xmlHttpRequest(或提取),则302不会重定向您的浏览器。 You need to retrieve the data and do a window.location = 'newUrlHere'; 您需要检索数据并执行一个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; 您的POST方法返回302,您查看标题,获取网址并设置窗口位置;
  • Your POST method returns an object or string in the body that has the new location you need to go. 您的POST方法会在正文中返回一个具有您需要使用的新位置的对象或字符串。 Get that and do window.location = newLocation; 取得并执行window.location = newLocation; and you're good to go. 而且你很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM