【问题标题】:Angular 2 with codeigniter api Does Not Working带有 codeigniter api 的 Angular 2 不起作用
【发布时间】:2018-06-29 15:26:35
【问题描述】:

我使用了 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());

    } 

【问题讨论】:

    标签: codeigniter angular2-services angular2-directives


    【解决方案1】:

    这可能是因为默认的 Angular 内容类型是 application/json,这将触发 preflighted request

    尝试在后端附加Content-Type 标头:

    header('Access-Control-Allow-Headers: X-Requested-With,Content-Type');
    

    【讨论】:

      猜你喜欢
      • 2017-12-12
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2016-12-21
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      相关资源
      最近更新 更多