不知道我在这里做错了什么。代码非常简单。
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class Service {
private baseUrl = 'http://localhost:8443';
constructor(private http: HttpClient) { }
callSecurityGateway(): Observable<String> {
const params = new HttpParams()
.set('grant_type', 'password')
.set('scope', 'read')
.set('username', 'myusername')
.set('password', 'mypassword');
const headers = new HttpHeaders().set('Authorization', 'Basic s89s89s89asd');
const httpOptions = {
headers: headers,
params: params,
responseType: 'text',
// withCredentials: true
};
// this works to ram the parameters in
// const oauthUrl = '/oauth/token?grant_type=password&scope=read&username=myusername&password=mypassword';
// return this.http.post<String>(this.baseUrl + oauthUrl, httpOptions);
return this.http.post<String>(this.baseUrl + '/oauth/token', httpOptions);
}
}
我在请求标头中看到的是:
OPTIONS /oauth/token HTTP/1.1
Host: localhost:8443
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/62.0.3202.89 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
我什至没有看到任何应该有的参数。
我不知道这是否有帮助,但我在httpOptions
控制台中的对象中看到的内容显示了标题headers.lazyUpdate
和参数,params.updates
其中是我设置的 4 个参数的数组。
我的例子已经用完了;http
认为向后退并使用而不是httpclient
为此可能更容易。