尝试从 ionic 3 项目 http.post 到 java 网络服务时出现错误 415 不支持的媒体类型
Error 415 Unsupported Media Type while trying to http.post from a ionic 3 project to a java webservice
我有一个 java 后端项目,它使用 JERSEY 和一个 ionic 3 项目作为前端。我无法使 post 方法工作,尽管 get 方法工作正常。
这是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 错误。我在这里缺少什么?
您收到 415 错误,因为服务器需要 application/json,并且您将内容类型设置为 application/x-www-form-urlencoded。
您收到 400,因为您发送的数据无效:服务器需要一些特定的 JSON 结构和值,而您发送的是其他内容。您应该在响应中有一条错误消息,告诉您出了什么问题。检查您发送的内容,并检查 Oferta 的结构应该是什么。
请注意,将 content-type header 设置为 application/json 是没有用的,因为 Angular 在发布 object.
我有一个 java 后端项目,它使用 JERSEY 和一个 ionic 3 项目作为前端。我无法使 post 方法工作,尽管 get 方法工作正常。
这是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 错误。我在这里缺少什么?
您收到 415 错误,因为服务器需要 application/json,并且您将内容类型设置为 application/x-www-form-urlencoded。
您收到 400,因为您发送的数据无效:服务器需要一些特定的 JSON 结构和值,而您发送的是其他内容。您应该在响应中有一条错误消息,告诉您出了什么问题。检查您发送的内容,并检查 Oferta 的结构应该是什么。
请注意,将 content-type header 设置为 application/json 是没有用的,因为 Angular 在发布 object.