gpt4 book ai didi

codeigniter - 飞行前响应中的 Access-Control-Allow-Headers 不允许 Angular 2 请求 header 字段授权

转载 作者:行者123 更新时间:2023-12-04 22:01:05 25 4
gpt4 key购买 nike

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

$config['rest_auth'] = 'basic';

$config['rest_valid_logins'] = ['uname' => 'pwd'];

对于 CORS 检查我也使用了下面的代码,

$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;

而且我已经在我的休息 Controller 中明确尝试过,

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');

我在 Angular 中使用了下面的代码,

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.';
}
}, () => {
});
}
}
}

当我与 postman 核实后,它运行良好,没有任何问题。当检查 Angular 错误时,

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.

如果我伪造其余身份验证并删除 Authorization header ,则无需检查 api 用户名和密码即可正常工作

$config['rest_auth'] = FALSE;

Angular

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

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

最佳答案

Authorization 添加到 CORS 检查中:

$config['allowed_cors_headers'] = [
'Authorization',
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'Access-Control-Request-Method'
];

关于codeigniter - 飞行前响应中的 Access-Control-Allow-Headers 不允许 Angular 2 请求 header 字段授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44213276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com