如何在IBM HTTP Server中为预检请求选项方法设置Response标头?

时间:2018-12-27 11:11:16

标签: java ajax apache dojo ibmhttpserver

我正在使用dojo / xhrpost(dojo 1.7)进行AJAX调用,出现此错误

  

在以下位置访问XMLHttpRequest   'https://dev.services.com/secure/ui/services/getEmployeeDetails/AdditionalContact'   来自来源“ https://dev.ead.com”的信息已被CORS政策阻止:   对预检请求的响应未通过访问控制检查:   预检请求不允许重定向。

尝试通过两种方法解决它

1)通过在Servlet过滤器类(Java)中添加标头。下面的代码段。

((HttpServletResponse) response).addHeader("Access-Control-Allow-Origin", "*");
          ((HttpServletResponse) response).addHeader("Access-Control-Allow-Methods","GET, OPTIONS, HEAD, PUT, POST");

            HttpServletResponse resp = (HttpServletResponse) response;

            if (request1.getMethod().equals("OPTIONS")) {
                resp.setStatus(HttpServletResponse.SC_ACCEPTED);
                return;
            }

         chain.doFilter(request1, resp);

2)通过在httpd.config文件中添加Directory,Location,Virtual Host标记。下面的代码段

LoadModule headers_module modules/mod_headers.so
<Directory ${APPCONTENT}/docroot>
AllowOverride None
Options Indexes  FollowSymLinks
Require all granted 

# Always set these headers.
Header always set Access-Control-Allow-Credentials "true"
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

</Directory>

################INCLUDE VIRTUALHOST################
Include ${APPCONF}/vhost.conf

附加信息:作为响应,获取 HTTP:302 ,预检请求标头同时具有这两个标记 访问控制请求标头:内容类型,x要求与 访问控制请求方法:POST

使用以上两种方法,我无法为该预检请求设置响应标头。请帮助我解决此问题。

0 个答案:

没有答案