设置标题将post方法转换为OPTIONS - Angular

时间:2018-02-19 11:14:36

标签: php angular http cors http-post

我正在尝试使用拦截器为授权用户设置标头。但是,设置标题会将post方法转换为选项。我已经阅读过关于预检请求和cors,但仍然无法解决它。想知道它是否与我有关或者应该从后端做些什么。

P.S:与Postman一样经过测试,有效。

这是我设置标题的拦截器:

import {Injectable} from "@angular/core";
import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpHeaders,
  HttpRequest
} from "@angular/common/http";
import {Observable} from "rxjs/Observable";
import {AuthService} from "../../auth/auth.service";
@Injectable()
export class SetTokenIfLoggedIn implements HttpInterceptor {
  constructor(private _authService: AuthService) {}

  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    let cloneReq = req;
    let headers = new HttpHeaders()
      .append("api-token", this._authService.getToken())
      .append("Accept", "application/json, text/plain, */*")
    if (this._authService.isLogged()) {
      cloneReq = req.clone({
        headers: headers
      });
    }
    return next.handle(cloneReq);
  }
}

错误讯息:

This is what I get as response

请求标题

Request:

回复标题

Response

0 个答案:

没有答案