CORS不能与tomcat + javascript一起使用

时间:2016-12-08 17:05:25

标签: javascript http tomcat xmlhttprequest cors

发送XMLHttpRequest时

function requireCS(){
var http = new XMLHttpRequest();
var url = "url";
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Request", "categories");

http.onreadystatechange = function() {changes.
  if(http.readyState == 4 && http.status == 200) {
  }
}
http.send();
}

我收到错误

  

XMLHttpRequest无法加载-serverurl-。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许原点'null'访问。响应具有HTTP状态代码403。   script.js:263 XHR加载失败:POST“-serverurl - ”。

我在web.xml中设置了标头

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  <init-param>
  <param-name>cors.allowed.origins</param-name>
   <param-value>*</param-value>
  </init-param>
  <init-param>
   <param-name>cors.allowed.methods</param-name>
   <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
  <param-name>cors.allowed.headers</param-name>
  <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-          Request-Method,Access-Control-Request-Headers</param-value>
  </init-param>
  <init-param>
  <param-name>cors.exposed.headers</param-name>
  <param-value>Access-Control-Allow-Origin,Access-Control-Allow-    Credentials</param-value>
  </init-param>
  <init-param>
  <param-name>cors.support.credentials</param-name>
  <param-value>true</param-value>
   </init-param>
   <init-param>
   <param-name>cors.preflight.maxage</param-name>
   <param-value>10</param-value>
  </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CorsFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

无法弄清楚什么是错的。 你能救我吗?

1 个答案:

答案 0 :(得分:0)

要让CORS与POST一起使用,您必须

  1. 设置内容类型(您正在做的事情)
  2. 发送虚拟有效载荷(!)
  3. 例如,这个JavaScript适用于Chrome(OSX中的v56)和Tomcat v7.0.75:

                var url = "http://localhost:8080/MyServer/MyServlet/a/b";
                var req = new XMLHttpRequest();
                req.open("POST", url, true);
                req.setRequestHeader("Content-type", "application/octet-stream");
                req.onreadystatechange = function () {
                    if (req.readyState == 4) {
                        var status = req.status;
                        var hundred = Math.floor(status / 100);
                        callback(hundred == 2);
                    }
                };
                var dummyPayload = new Uint8Array(1);
                req.send(dummyPayload);
    

    这是我在web.xml中成功的Tomcat CORS配置:

    <filter>
      <filter-name>CorsFilter</filter-name>
      <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
      <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,PUT</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
      </init-param>
      <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>*</param-value>
      </init-param>
      <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>true</param-value>
      </init-param>
      <init-param>
        <param-name>cors.preflight.maxage</param-name>
        <param-value>180</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    我也得到了PUT来使用上面的Tomcat配置和以下JS:

                var url = "http://localhost:8080/MyServer/MyServlet/a/b";
                var req = new XMLHttpRequest();
                req.open("PUT", url, true);
                req.responseType = "text";
                req.onreadystatechange = function () {
                    if (req.readyState == 4) {
                        var status = req.status;
                        var hundred = Math.floor(status / 100);
                        callback(hundred == 2);
                    }
                };
                var text="Hello World";
                req.send(text);
    

    我不声称理解上述原因。但是如果我的POST和PUT示例适合你,那么你可以让它们适应你想做的一点点。