Вы не можете напрямую добавлять пользовательские заголовки с помощью window.open () во всплывающем окне, но для работы у нас есть два возможных решения
- Напишите метод Ajax для вызова этого конкретного URL с заголовками в отдельном файле HTML и используйте этот HTML в качестве URL-адреса в
<i>window.open()</i>
здесь: 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) {
} });
html
window.open('*\abc.html')
здесь политика CORS может блокировать запрос, если CORS не включен в запрошенном URL .
Вы можете запросить URL, который запускает серверную программу, которая выполняет запрос с пользовательскими заголовками, а затем возвращает ответ, перенаправляющий на этот конкретный URL.
Предположим, в сервлете Java (/ 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");
}
`
вызов сервлета в window.open('/requestURL')