【发布时间】:2018-04-05 20:52:03
【问题描述】:
我有一个使用 JERSEY 的 java 后端项目和一个用于前端的 ionic 3 项目。尽管 get 方法工作正常,但我无法让 post 方法工作。
这是post方法>
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/")
public Response create(Oferta oferta) throws SQLException, ClassNotFoundException{
ofertaDAO dao = new ofertaDAO();
dao.insert(oferta);
return Response
.status(200)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
.header("Access-Control-Max-Age", "1209600")
.entity(oferta)
.build();
}
我尝试以两种不同的方式在我的 ionic 项目中执行我的 post 功能,这:
postData(params){
let headers = new Headers();
headers.append('Content-type','application/json');
return this.http.post(this.api,params,{
headers: headers
}).map(
(res:Response) => {return res.json();}
);
}
这样 >
postData(params){
let headers = new Headers({'Content-type' : 'application/x-www-form-urlencoded'});
return this.http.post(this.api,params,{
headers: headers,
method: 'POST'
}).map(
(res:Response) => {return res.json();}
);
}
我得到 http 400 错误的第一种方式,我得到 415 错误的第二种方式。我在这里缺少什么?
【问题讨论】: