2017-08-15 70 views
0

POST请求时收到错误我是很新的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 ;  
} 

这在邮递员

工作正常,但是当我做post request

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'因此不允许访问。响应有HTTP状态代码在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> 
+0

你需要[用一些init-params配置CorsFilter](https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter)告诉它你想要的起源。 –

+0

@peeskillet \t我在tomcat中配置了Apache CorsFilter – user6688672

+0

在您的过滤器中添加了“Access-Control-Allow-Origin”,“*” – Mankeomorakort

回答

0

使用403

过滤器您正试图http://localhost:8080/使用XMLHttpRequest的从本地主机进行通信:8100这是默认不允许的。通过将其添加到访问控制允许来源响应头8100: 您的服务器应该相信为localhost

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

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

+0

我在tomcat中配置了Apache CorsFilter – user6688672

+0

不,它的init参数没有配置。看看[this](https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter/Initialisation_parameters)示例。 –