我使用了角度2前端我将我的api请求发送到codeigniter我在控制台中得到以下错误
Failed to load http://localhost/cli-scr1/api/login: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
这是我的api通话功能,
islogin(info){
console.log(info);
return this.http.post("http://localhost/cli-scr1/api/login",info)
.map(res => {
if(res.json()){
var arr = Object.values(res.json()[0]);
console.log(arr[0]['name']);
localStorage.setItem("userName",arr[0]['name']);
this.router.navigate(['/home']);
}else{
this.router.navigate(['/login']);
}
}
);
}
这是我的codeigniter功能,
public function index()
{
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
print_r($this->input->post());
}
答案 0 :(得分:2)
可能会发生这种情况,因为角度的默认内容类型为application/json
,会触发preflighted request。
尝试在后端添加Content-Type
标题:
header('Access-Control-Allow-Headers: X-Requested-With,Content-Type');