【问题标题】:ERROR:options (url) 405 (method not allowed) and failed to load (url):response for preflight has invalid HTTP status code 405错误:选项(url)405(不允许方法)并且加载失败(url):预检响应具有无效的HTTP状态代码405
【发布时间】:2018-02-06 20:14:37
【问题描述】:

我尝试使用 angular 5 中的 httpClient 从 api 获取数据,但出现错误。以下 *1)ERROR:options http://localhost:8080/api/getdata 405 (方法不允许)

2)ERROR:failed to load http://localhost:8080/api/getdata 405 (method not allowed):response for preflight has invalid HTTP status code 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});
}
}

我哪里做错了?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    OPTIONS 请求应该从服务器端处理。 当您请求远程资源时,浏览器会在实际请求发生之前发出 OPTIONS 请求。

    OPTIONS 请求将检索允许的标头,如果您尚未在“Acess-Control-Allow-Headers”中添加标头,则会抛出不允许“某些标头”的错误。

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 2017-04-13
      • 2016-06-30
      • 2016-08-03
      • 2016-09-19
      • 1970-01-01
      • 2016-10-10
      • 2018-06-02
      • 2016-08-24
      相关资源
      最近更新 更多