Angular2 POST不向服务器发送数据

时间:2016-10-02 15:28:32

标签: ajax servlets angular

我有一个服务,它将一些http请求(所有帖子)发送到java servlet。在servlet中,我从下面的代码中打印出method变量。我有两个请求方法工作得很好,但我做了第三个,服务器没有收到任何东西(可能)。

服务

import {Injectable} from '@angular/core';
import {Employee} from './employee';

import {Observable} from 'rxjs/Rx';
import {Http, Response, Headers, RequestOptions} from '@angular/http';

@Injectable()
export class ManagementService{

private url = 'http://localhost:8080/whatever';

constructor(private http: Http) { }

getDepartments(): Observable<string[]> {

    let method = "getDepartments";
    let body = JSON.stringify({method});
    console.log(body);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.post(this.url, body, options)
                    .map((res:Response) =>{ 
                            console.log(res.json());
                            return res.json();
                    }).catch(this.handleError);
}

getEmployees(department: string): Observable<Employee[]> {

        let method = "getEmployees";
        let body = JSON.stringify({method,department});
        console.log(body);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        return this.http.post(this.url, body, options)
                        .map((res:Response) =>{ 
                                console.log(res.json());
                                return res.json();
                        }).catch(this.handleError);
}

deleteEmployee(employee: Employee): any{

        let method = "deleteEmployee";
        let body = JSON.stringify({method,employee});
        console.log(body);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        return this.http.post(this.url, body, options)
                        .map((res:Response) =>{ 
                                console.log(res.json());
                                return res.json();
                        }).catch(this.handleError);
}


private handleError (error: any) {
     let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
     console.error(errMsg);
     return Observable.throw(errMsg);
}

}

0 个答案:

没有答案