繁体   English   中英

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

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

我有一个使用angularjs 1.6和Java 8的应用程序。我想将POST数据发送到另一个应用程序,然后转到该应用程序确定的URL。

域是我的应用程序发送想要进行一项服务的公民的数据。 另一个应用程序,获取此数据并显示带有表单的视图,该表单的所有字段均自动填写我发送的数据。 我必须这样做,因为用户需要它。

我尝试了几种方法来执行此操作,例如使用xmlHttpRequest,但是当我发送帖子时,用户页面未重定向(尽管接收状态为302)。

我尝试过的可能解决方案

$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
            });

另一个:

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

我该怎么做?

谢谢!

如果您使用的是xmlHttpRequest(或提取),则302不会重定向您的浏览器。 您需要检索数据并执行一个window.location = 'newUrlHere'; 每当您的电话会议结束时。

您可以通过多种方式进行操作,其中有两种:

  • 您的POST方法返回302,您查看标题,获取网址并设置窗口位置;
  • 您的POST方法会在正文中返回一个具有您需要使用的新位置的对象或字符串。 取得并执行window.location = newLocation; 而且你很好。

暂无
暂无

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

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