Access-Control-Allow-Origin不允许原点没有解决方案

时间:2017-03-01 11:18:41

标签: angularjs json rest cors jersey-2.0

我试图解决以下问题超过10天。我在没有解决方案的情况下从该站点中删除所有指南和页面 我在java中使用Jersey库开发了一个REST Web服务器。该服务的结果是JSON格式。在我的服务器中,我添加了CORS过滤器

服务器

package server;

import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;

public class CORSFilter implements ContainerResponseFilter {

   @Override
   public void filter(final ContainerRequestContext requestContext,
                      final ContainerResponseContext cres) throws IOException {
      cres.getHeaders().add("Access-Control-Allow-Origin", "*");
      cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
      cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
      cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
      cres.getHeaders().add("Access-Control-Max-Age", "1209600");
   }

}

并且web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://xmlns.jcp.org/xml/ns/javaee"     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>conferenceServer</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-    class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>server</param-value>
        </init-param>

        <init-param>
            <param-    name>javax.ws.rs.container.ContainerResponseFilter</param-name>
            <param-value>server.CORSFilter</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

响应的JSON格式化程序有效,但是当我在我的应用程序中调用该服务时,收到此消息错误:

[Error] Failed to load resource: Origin http://localhost:8100 is not allowed by Access-Control-Allow-Origin. (all, line 0)
[Error] XMLHttpRequest cannot load http://****/myServer/service. Origin http://localhost:8100 is not allowed by Access-Control-Allow-Origin. 

在您看来,问题出在哪里?

1 个答案:

答案 0 :(得分:0)

我不认为这可以作为一个参数名称

@Provider

您可以采取两项措施来确保过滤器已注册:

  1. 在过滤器类的顶部添加jersey.config.server.provider.packages 。 init-param

    ...classnames

    将确保从包扫描中获取并注册。

  2. 使用init-param <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>server.CORSFilter</param-value> </init-param> 注册过滤器

    @app.route('/delete_todo/<int:todo_id>', methods=['POST'])
    @login_required
    def delete_todo(todo_id):
        g.db.execute('''delete * from todo where todo_id = ?''', [todo_id])
        g.db.commit()
        flash('Your message was deleted')
        return redirect(url_for('index'))