我试图在角度5中使用httpClient从api获取数据但是我遇到了错误。下面 * 1)错误:选项http://localhost:8080/api/getdata 405(方法不允许)
2)错误:无法加载http://localhost:8080/api/getdata 405(方法不允许):预检的响应包含无效的HTTP状态代码405 *
以下是我的服务代码
import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse,HttpHeaders } from '@angular/common/http';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class DataService {
url = "http://localhost:8080/api/getdata";
constructor (private httpClient: HttpClient) {}
getList() {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json').set('Access-Control-Allow-Origin','*').set('accept', 'application/json').set('Authorization', 'Basic bWdpcxxdferhWx1Xzkz');
return this.httpClient.get(this.url, {headers});
}
}
我在哪里犯了错误?
答案 0 :(得分:0)
OPTIONS请求应该从服务器端处理。 当您请求远程资源时,浏览器会在实际请求发生之前发出OPTIONS请求。
OPTIONS请求将检索允许的标头,如果您尚未在“Acess-Control-Allow-Headers”中添加标头,那么它将抛出错误,即“some-header”不允许。