在离线角度

时间:2017-08-15 16:03:12

标签: angular tomcat jax-rs ionic3

我对ionic3和jaxrs非常陌生,所以请以正确的方式指导我学习

    @POST
    @Path("example1")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public List<DairyDTO> getTodaydairyForParents(EmployeeDTO employee) {
        empList = employeeDAO.getRecords(employee.getname());


    return empList ;    
}

这在邮递员工作正常

但是当我提出要求时

let employee = new URLSearchParams();
employee.set('name', "Ram");

this.http.post('http://localhost:8080/sms/webapi/test/example1', employee, {
            headers: headers
        }).map(res => res.json())
      .subscribe(data => {
        // we've got back the raw data, now generate the core schedule data
        // and save the data for later reference
        this.data = data;
        resolve(this.data);
      });

i am getting errors 

XMLHttpRequest cannot load localhost:8080/restexample/webapi/test/example1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin localhost:8100&#39;因此不允许访问。响应的HTTP状态代码为403。

tomcat中使用的过滤器

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

1 个答案:

答案 0 :(得分:0)

您正尝试使用localhost:8100中的XMLHttpRequest与http://localhost:8080/进行通信,默认情况下不允许这样做。 您的服务器应该信任localhost:8100,方法是将其添加到Access-Control-Allow-Origin响应头:

Access-Control-Allow-Origin: http://localhost:8100

您应该根据您需要的访问策略配置Apache CorsFilter参数。