我是Angular2中的新成員。我正在嘗試從Angular2調用簡單的Java REST調用。當我發佈數據時,我根本沒有收到錯誤,但是我的java post方法沒有被調用。POST方法不適用於Angular2
Angular2 POST-
let emplyee = JSON.stringify(this.object);
let url = 'http://localhost:8080/rest-app/rest/employeeService/insertEmployee';
console.log("Data: " + emplyee);
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
this.http.post(url, emplyee, options);
Java的POST方法 -
@POST
@Path("/insertEmployee")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String insertEmployee(String employee) {
logger.debug("Employee: " + employee);
return "Hello";
}
你得到的迴應是什麼?在'this.http.post()'後面加'.subscribe(res => console.log(res))到'console.log'這個響應。 – Supamiu
@Supamiu thnx for response,我收到了這個迴應,**對預檢請求的響應沒有通過訪問控制檢查:在請求的資源上沒有「Access-Control-Allow-Origin」標頭。因此不允許訪問Origin'http:// localhost:3000'。** – deen