不确定我在这里做错了什么。代码非常简单。
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
。
答案 0 :(得分:0)
如果Access-Control-Allow-Headers
上不允许Access-Control-Allow-Credentials: true
标题,您将收到错误消息。如果您没有header('Content-Type: text/plain');
if( isset($_SERVER['HTTP_ORIGIN']) )
{
header('Access-Control-Allow-Origin: ' . trim($_SERVER['HTTP_ORIGIN']));
}else{
header('Access-Control-Allow-Origin: *');
}
header('Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Credentials: true');
// Client/Browser may send 'OPTIONS' header to check its allowed or not
if( $_SERVER['REQUEST_METHOD'] == 'OPTIONS' )
{
return;
}
在您的服务器端,尝试添加此代码以制作&#39;凭据&#39;在跨站点工作:
@Table