从角度5向服务器发出POST请求时出错

时间:2018-03-22 15:33:48

标签: html angular rest post

当我向服务器发出POST请求时,它会返回以下错误:

ERROR 对象{headers:{...},状态:400,statusText:“OK”,url:“serverURL”,ok:false,名称:“HttpErrorResponse”,消息:“serverURL的Http失败响应:400 OK”,错误:null }

我成功地将DELETE请求发送到同一个URL,我可以使用浏览器访问数据。 POST请求如下所示:

    private postrequest() {
            const httpOptions = {
            headers: new HttpHeaders({
                'Content-Type':  'application/json',
            })
        };
            let body = {
       firstname:this.myarray[0],
       host:(this.myarray[1]+':'+this.myarray[2])
       }

       return this.http.post(this.serverURL, body, httpOptions ).subscribe(res => console.log(res));
   }

成功的DELETE请求如下所示:

private deleterequest() {
        for (var i = this.delArray.length - 1; i >= 0; i--) {
            if ((this.delArray[i]) !== undefined) {
                const httpOptions = {
                headers: new HttpHeaders({
                    'Content-Type':  'application/json',
                })
            };
                this.http.delete(this.serverURL  + this.delArray[i], httpOptions).subscribe(res => console.log(res));

            }
        }
        this.getPeers();
    }

1 个答案:

答案 0 :(得分:0)

您忘记添加选项

private postrequest() {
    let headers = new Headers({ 'Content-Type': 'application/json','Accept': 'q=0.8;application/json;q=0.9'});
    let options = new RequestOptions({headers: headers});
    let search = new URLSearchParams();
    this.http.post(this.serverURL, {firstname:this.myarray[0], host:(this.myarray[1]+':'+this.myarray[2])}, options).subscribe(res => console.log(res));
}