Angular 2请求标头字段在飞行前响应中,Access-Control-Allow-Headers不允许授权

Vin*_*mar 2 codeigniter codeigniter-3 codeigniter-restserver angular

我正在为服务器应用程序使用codeigniter REST API。客户端为Angular2,在我的REST API中,我给出了基本身份验证。我已设定为

$config['rest_auth'] = 'basic';
Run Code Online (Sandbox Code Playgroud)

$config['rest_valid_logins'] = ['uname' => 'pwd'];
Run Code Online (Sandbox Code Playgroud)

对于CORS Check,我也使用了以下代码,

$config['check_cors'] = TRUE;
$config['allowed_cors_headers'] = [
  'Origin',
  'X-Requested-With',
  'Content-Type',
  'Accept',
  'Access-Control-Request-Method'
];
$config['allowed_cors_methods'] = [
  'GET',
  'POST',
  'OPTIONS',
  'PUT',
  'PATCH',
  'DELETE'
];
$config['allow_any_cors_domain'] = TRUE;
Run Code Online (Sandbox Code Playgroud)

我也尝试过在我的rest控制器中尝试过,

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
Run Code Online (Sandbox Code Playgroud)

我在下面的代码中使用了角度

import { Component, OnInit } from '@angular/core';
import { Http, Headers, RequestOptions  } from '@angular/http';
import { FormGroup, AbstractControl, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { CommonService, BaseAPIURL } from '../common/common.service';
export class Login implements OnInit {
 private headers: Headers;
  constructor(fb: FormBuilder, private router: Router, private http: Http,
    private commonService: CommonService) {
     this.headers = new Headers();
     this.headers.append('Authorization', 'Basic ' + btoa('uname:pwd'));
     this.headers.append('Content-Type', 'application/json');

  }
 ngOnInit() {
  }

  public onSubmit(values: Object): void {
    this.submitted = true;
    if (this.form.valid) {
      let options = new RequestOptions({ headers: this.headers });
      this.http.post(this.getuserLoginURL, JSON.stringify(values),  options ).subscribe(
        response => {
          let result = response.json();
          this.errorMessage = result.message;
        },
        error => {
          this.isDisabled = false;console.log(error);
          if (error.status == 400) {
            this.errorMessage = JSON.parse(error._body).message;
          } else {
            this.errorMessage = 'Internal server error. please contact admin.';
          }
        }, () => {
        });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我向邮递员查询时,它工作正常,没有任何问题。当检查角度误差时,

XMLHttpRequest cannot load http://localhost:97/sencogold/index.php/Adminaccount_api/login_adminuser. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
Run Code Online (Sandbox Code Playgroud)

如果我虚假了其余身份验证并删除了Authorization标头,则无需检查api用户名和密码即可正常运行

$config['rest_auth'] = FALSE;
Run Code Online (Sandbox Code Playgroud)

并成角度

 this.headers = new Headers();
    //this.headers.append('Authorization', 'Basic ' + btoa('lmxretail:lmx@2017'));
    this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
Run Code Online (Sandbox Code Playgroud)

请帮助任何人为我的api申请身份验证。

geo*_*awg 5

添加Authorization到CORS检查:

$config['allowed_cors_headers'] = [
  'Authorization',
  'Origin',
  'X-Requested-With',
  'Content-Type',
  'Accept',
  'Access-Control-Request-Method'
];
Run Code Online (Sandbox Code Playgroud)