Angular 2 with codeigniter api 不工作
Angular 2 with codeigniter api Does Not Working
我使用了 angular 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());
}
可能会发生这种情况,因为 angular 的默认内容类型是 application/json
,这将触发 preflighted request。
尝试在后端追加 Content-Type
header :
header('Access-Control-Allow-Headers: X-Requested-With,Content-Type');
我使用了 angular 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());
}
可能会发生这种情况,因为 angular 的默认内容类型是 application/json
,这将触发 preflighted request。
尝试在后端追加 Content-Type
header :
header('Access-Control-Allow-Headers: X-Requested-With,Content-Type');