关于带有标题的javascript:window.open

window.open with headers

我可以控制window.open(跨浏览器)发送的HTTP标头吗?

如果不是,我可以以某种方式window.open一个页面,该页面然后在其弹出窗口中发出带有自定义标头的请求吗?

我需要一些狡猾的技巧。


Can I control the HTTP headers sent by window.open (cross browser)?

没有

If not, can I somehow window.open a page that then issues my request with custom headers inside its popped-up window?

  • 您可以请求一个URL,该URL触发一个服务器端程序,该程序使用任意标头发出请求,然后返回响应
  • 您可以运行使用XHR来使用任意标头(假定URL符合Same Origin Policy中的要求)进行请求的JavaScript(可能与渐进增强说再见),然后在JS中处理结果。

I need some cunning hacks...

如果您描述问题而不是询问是否可能的解决方案可能会有所帮助。


如果您在服务器端控制,则可能可以在查询字符串中设置标头值并像这样发送它?
这样,如果在标头中找不到查询字符串,则可以从查询字符串中对其进行解析。

只是一个主意...而您要求一个狡猾的技巧:)


正如最好的anwser用window.open以外的XMLHttpResponse编写的一样,我将abstracts-anwser作为实例。

主要Js文件是download.js下载JS

1
2
3
4
5
6
7
8
9
10
11
12
13
 // var download_url = window.BASE_URL+"/waf/p1/download_rules";
    var download_url = window.BASE_URL+"/waf/p1/download_logs_by_dt";
    function download33() {
        var sender_data = {"start_time":"2018-10-9","end_time":"2018-10-17"};
        var x=new XMLHttpRequest();
        x.open("POST", download_url, true);
        x.setRequestHeader("Content-type","application/json");
//        x.setRequestHeader("Access-Control-Allow-Origin","*");
        x.setRequestHeader("Authorization","JWT" + localStorage.token );
        x.responseType = 'blob';
        x.onload=function(e){download(x.response,"test211.zip","application/zip" ); }
        x.send( JSON.stringify(sender_data) ); // post-data
    }

You can't directly add custom headers with window.open() in popup window
but to work that we have two possible solutions

  • Write Ajax method to call that particular URL with headers in a separate HTML file and use that HTML as url inwindow.open()
    here is abc.html
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
            $.ajax({
            url:"ORIGIONAL_URL",
            type: 'GET',
            dataType: 'json',
            headers: {
                Authorization : 'Bearer ' + data.id_token,
                AuthorizationCheck : 'AccessCode ' +data.checkSum ,
                ContentType :'application/json'
            },

            success: function (result) {
                  console.log(result);
            },
            error: function (error) {

            } });

    调用html

    1
    window.open('*\abc.html')

    here CORS policy can block the request if CORS is not enabled in requested URL.

  • You can request a URL that triggers a server-side program which makes the request with custom headers and then returns the response redirecting to that particular url.
  • 假设在Java Servlet(/ requestURL)中,我们将发出此请求

    `

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
            String[] responseHeader= new String[2];
            responseHeader[0] ="Bearer" + id_token;
            responseHeader[1] ="AccessCode" + checkSum;

            String url ="ORIGIONAL_URL";

            URL obj = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) obj.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type","application/json");
            urlConnection.setRequestProperty("Accept","application/json");
            urlConnection.setRequestProperty("Authorization", responseHeader[0]);
            urlConnection.setRequestProperty("AuthorizationCheck", responseHeader[1]);
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new
                             InputStreamReader(urlConnection.getInputStream()));
                String inputLine;
                StringBuffer response1 = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response1.append(inputLine);
                }
                in.close();
                response.sendRedirect(response1.toString());
                // print result
                System.out.println(response1.toString());
            } else {
                System.out.println("GET request not worked");
            }

    `

    window.open('/requestURL')中调用servlet