angular http post with 'Content-Type': 'application/x-www-form-urlencoded' and responseType: 'text'

angular http post with 'Content-Type': 'application/x-www-form-urlencoded' and responseType: 'text'

这是我的 angular cli 信息:

Angular CLI: 7.3.9
Node: 10.19.0
OS: win32 x64
Angular: 7.2.16
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.9
@angular-devkit/build-angular     0.13.9
@angular-devkit/build-optimizer   0.13.9
@angular-devkit/build-webpack     0.13.9
@angular-devkit/core              7.3.9
@angular-devkit/schematics        7.3.9
@angular/cdk                      7.3.7
@angular/cli                      7.3.9
@angular/material                 7.3.7
@ngtools/webpack                  7.3.9
@schematics/angular               7.3.9
@schematics/update                0.13.9
rxjs                              6.5.4
typescript                        3.2.4
webpack                           4.29.0    

我有一个从服务器检索票证的方法。

getTGT(username, password): Observable<string> {
    const loginUrl = this._constants.getLoginUrl();

    let body = new URLSearchParams();
    body.set('username', username.trim());
    body.set('password', password);

    const options = {
        headers: new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'}),
        responseType: 'text'
    };

    return this.http.post(loginUrl, body.toString(), options)
        .map((res) => {
            return res.toString();
        })
        .catch(this.handleError);
}

但我遇到类型脚本编译错误:

Argument type {headers: HttpHeaders, responseType: string} is not assignable to parameter type {headers?: HttpHeaders | {[p: string]: string | string[]}, observe?: "body", params?: HttpParams | {[p: string]: string | string[]}, reportProgress?: boolean, responseType: "arraybuffer", withCredentials?: boolean}

如何修复此类型错误? 谢谢!

我最终使用 fetch API:

解决了这个问题

`

import { from } from 'rxjs';

fetchTgtUrl(username, password) {
    let loginUrl = '' + this._constants.getLoginUrl();
    let bodyString = 'username=' + username.trim() + '&password=' + encodeURIComponent(password);
    return fetch(loginUrl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: bodyString
    }).then(res => res.text());
}
getTGT(username, password): Observable<string> {
    let tgt = this.fetchTgtUrl(username, password);
    return from(tgt);
}

`