我知道这个问题已被反复询问过。但他们都没有帮助我。 我正在尝试将数据从角度4发布到REST API服务。但是在单击“提交”按钮后,其给出的错误为“无法加载" URL":预检的响应无效(重定向)”。 我知道这是CORS问题。我们也在web.xml中添加了标题。之后我们可以执行GET请求,但是获得POST,PUT和DELETE的Preflight错误。
以下是我的Component.ts方法。我在按钮提交点击时调用saveDetails(form.value)。 emergencycontacts是我的EMTServiceService(服务)
saveDetails(user) {
this.emergencycontacts.SaveEmpDetails([user])
.subscribe(
empData => {this.empDetails = empData;},
error => console.log(error)
);
EMTServiceService -
SaveEmpDetails(user): Observable<Employee> {
console.log(this.apiService.post('/Emt/saveEmtDetailsPost', user));
const some = this.apiService.post(
'/Emt/saveEmtDetailsPost', user
);
console.log({some});
return some.map( data => { return data.DATA; } );
}
apiService是:
post(path: string, body: Object = {}): Observable<any> {
let jsonPostValue = JSON.stringify(body);
return this.http.post(
`${environment.api_url}${path}`,
jsonPostValue,
{ headers: this.setHeadersPost() }
)
.catch(this.formatErrors)
.map((res: Response) => res.json());
}
休息服务方法:
@POST
@Path("/saveEmtDetailsPost")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String saveEmtDetails(EmtContacts EmtContactsBean) {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
String insertStmt = "INSERT INTO Emt_Contacts (EmpId,LocationID,DeptID,CreatedBy,CREATEDON,ModifiedBy,ModifiedOn,IsActive,Nortel,Mobile)"
+ "VALUES (?,?,?,?,?,?,?,?,?,?)";
System.out.println("insertStmt" + insertStmt);
try {
getJdbcTemplate().update(insertStmt, new Object[] {
EmtContactsBean.getEmpID(), EmtContactsBean.getLocationId(), EmtContactsBean.getDeptId(),
"ID5020","2017-01-01","ID5020","2017-01-01",
GlobalConstants.STATUS_ACTIVE,EmtContactsBean.getNortel(),EmtContactsBean.getMobile() });
jsonObject = RespBuilder.buildSuccessResponse(GlobalConstants.KEY_CODE_SUCCESS,
GlobalConstants.STATUS_SUCCESS_MESSAGE);
} catch (Exception e) {
AppLoger.APPLOGGER.info("Exception is :" + e);
jsonObject = RespBuilder.buildFailedResponse(GlobalConstants.KEY_MESSAGE_FAILURE, e.toString());
}
jsonArray.put(jsonObject);
System.out.println(jsonArray);
return jsonArray.toString();
}
提前感谢您的帮助