Я использую код javascript для вызова soap webservice, развернутого на weblogic и разработанного в java с использованием jaxws. Из IE11 я получаю ответ от SOAP, но из chrome я становлюсь ниже ошибки:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4200' is therefore not allowed access.
The response had HTTP status code 405.
Я добавил расширение cors в chrome, а затем я получил ниже новой ошибки:
Response for preflight has invalid HTTP status code 405
Я также устанавливаю заголовки запросов, как показано ниже:
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'X-Custom-Header');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,PUT,DELETE');
но до сих пор нет успеха.
Ниже приведен фрагмент кода для вызова моих веб-сервисов, развернутых в weblogic
var soapMessage = this.createSOAPMessage();
function createCORSRequest(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else {
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('POST','http://IPAddress:7001/Path/ServiceImplService?WSDL');
if(!xhr){
console.log('XHR Issue');
return;
}
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'X-Custom-Header');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,PUT,DELETE');
xhr.send(soapMessage);
var result
xhr.onload = function(){
result = xhr.responseText;
console.log('===Result====> '+result);
}
return result;
Создайте этот класс в своей службе:
@Provider
@PreMatching
public class CorsFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestCtx, ContainerResponseContext responseCtx) throws IOException {
responseCtx.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
responseCtx.getHeaders().add("Access-Control-Allow-Origin", "*");
responseCtx.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseCtx.getHeaders().add("Access-Control-Allow-Methods", "GET, POST");
}
}
Access-Control-Allow-Headers
также понадобятся Access-Control-Allow-Origin
, Access-Control-Allow-Headers
и Access-Control-Allow-Methods
если OP не удаляет эти (бессмысленные) заголовки из запрос
I also set request headers as below:
- вы не отправляете эти заголовки, они должны быть получены для CORS ... CORS контролируется сервером, клиент не может сказать серверу, чтобы он предоставил мне ваш ресурс, независимо от ваших настроек : п