[英]How To Send Request Authorization Content-type application/x-www-form-urlencoded
[英]angular http post with 'Content-Type': 'application/x-www-form-urlencoded' and responseType: 'text'
这是我的角度 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);
}
但我有一个类型脚本编译错误:
参数类型 {headers: HttpHeaders, responseType: string} 不可分配给参数类型 {headers?: HttpHeaders | {[p: 字符串]: 字符串 | string[]},observe?: "body", params?: HttpParams | {[p: 字符串]: 字符串 | 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);
}
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.