Javascript window.open 带标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4325968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:54:55  来源:igfitidea点击:

window.open with headers

javascriptwindow.open

提问by Andrew Bullock

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

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

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

如果没有,我能否以某种方式创建window.open一个页面,然后在其弹出窗口中使用自定义标题发出我的请求?

I need some cunning hacks.

我需要一些狡猾的黑客。

采纳答案by Quentin

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

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

No

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

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

  • You can request a URL that triggers a server side program which makes the request with arbitrary headers and then returns the response
  • You can run JavaScript (probably saying goodbye to Progressive Enhancement) that uses XHR to make the request with arbitrary headers (assuming the URL fits within the Same Origin Policy) and then process the result in JS.
  • 您可以请求触发服务器端程序的 URL,该程序使用任意标头发出请求,然后返回响应
  • 您可以运行 JavaScript(可能与渐进增强说再见),它使用 XHR 发出带有任意标头的请求(假设 URL 符合同源策略),然后在 JS 中处理结果。

I need some cunning hacks...

我需要一些狡猾的黑客...

It might help if you described the problem instead of asking if possible solutions would work.

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

回答by Igor

If you are in control of server side, it might be possible to set header value in query string and send it like that? That way you could parse it from query string if it's not found in the headers.

如果您控制服务器端,是否可以在查询字符串中设置标头值并像这样发送?这样,如果在标题中找不到它,您就可以从查询字符串中解析它。

Just an idea... And you asked for a cunning hack :)

只是一个想法......你要求一个狡猾的黑客:)

回答by actanble

As the best anwser have writed using XMLHttpResponseexcept window.open, and I make the abstracts-anwser as a instance.

作为最好的 anwser 使用XMLHttpResponseexcept编写的window.open,我将abstracts-anwser 作为一个实例。

The main Js file is download.jsDownload-JS

主要的Js文件是download.jsDownload-JS

 // 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
    }

回答by sunil bishnoi

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

您不能在弹出窗口中使用window.open()直接添加自定义标题,但我们有两种可能的解决方案



  1. Write Ajax method to call that particular URL with headers in a separate HTML file and use that HTML as url in<i>window.open()</i>here is abc.html
  1. 编写 Ajax 方法以在单独的 HTML 文件中调用带有标题的特定 URL,并在<i>window.open()</i>此处使用该 HTML 作为 url是abc.html
        $.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) {

        } });

call html

调用html

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

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

如果请求的 URL 中未启用 CORS,则这里 CORS 策略可以阻止请求



  1. 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.
  1. 您可以请求触发服务器端程序的 URL,该程序使用自定义标头发出请求,然后返回重定向到该特定 url 的响应。

Suppose in Java Servlet(/requestURL) we'll make this request

假设在 Java Servlet(/requestURL) 中我们会发出这个请求

`

`

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

`

`

call servlet in window.open('/requestURL')

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