2017-08-15 63 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)示例。 –