簡體   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