如何捕获api请求的重定向URL中包含的URL参数

时间:2019-01-21 16:59:18

标签: javascript

我是API请求和JavaScript的新手... 一个API请求给了我一些我想捕获的参数,但是它们在生成的重定向URL中……我如何捕获它们?

在这里,我的实际代码是"link" = my API request url

fetch(link)
        .then(response => response.text())
        .then((data) => {
          console.log(data);
        });

那样,我只能在某处访问html响应的文本格式

<html><body><script> var url = THE_URL_OF_REDIRECTION_WITH_PARAMS_I_WANT_TO_CATCH

回复示例:

<html lang="fr">
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
        <p>Veuillez patienter...</p>
        <script type="text/javascript">
        /**
         * Delete cookies (iPlanetDirectoryPro, amlbcookie)
         */
        function removeCookie(cookieName) {
            cookieValue = "";
            cookieLifetime = -1;
            var date = new Date();
            date.setTime(date.getTime() + (cookieLifetime*24*60*60*1000));
            var expires = "; expires=" + date.toGMTString();
            document.cookie = cookieName+"=" + JSON.stringify(cookieValue) + expires+"; path=/; domain=.ene.fr";
        }
        removeCookie("iPlanetDirectoryPro");
        removeCookie("amlbcookie");

        /**
         * Parse url query
         */
        var parseQueryString = function( queryString ) {
            var params = {}, queries, temp, i, l;
            // Split into key/value pairs
            queries = queryString.split("&");
            // Convert the array of strings into an object
            for ( i = 0, l = queries.length; i < l; i++ ) {
                temp = queries[i].split('=');
                params[temp[0]] = temp[1];
            }
            return params;
        };
        
        // THE_URL_OF_REDIRECTION_WITH_PARAMS_I_WANT_TO_CATCH :
        var url = "https://gw.hml.api.ene.fr/redirect?code=qhTyG5h6bfdPktWggLidemqFDwkwl2&state=fz80ac780&usage_point_id=56416914714165";

        // Get redirect_uri params
        var queryString            = url.substring( url.indexOf('?') + 1 );
        var params                 = parseQueryString(queryString);
        var forceNonAutomaticClose = params.close !== undefined && params.close == 'false';

        // Avoid closing popup
        if (forceNonAutomaticClose || !this.window.opener || this.window.opener.closed) {
            this.window.location.href = url;
        } else if (this.window.opener && !this.window.opener.closed) {
            // Close popup
            this.window.opener.location.href = url;
            this.window.close();
        }
    
        </script>
    </body>
</html>

我如何完成我的代码?

谢谢!

0 个答案:

没有答案