请求标头字段在飞行前响应中,Access-Control-Allow-Headers不允许Access-Control-Allow-Origin。在CodeIgniter框架中

问题描述 投票:0回答:1

是codeIgniter的新手,我已经使用code-igniter构建了REST Web服务。

使用react JS的UI。

在邮递员网络服务中似乎工作正常。但是,当我集成到React JS中时,它会抛出错误。

无法加载http://123.0.0.5:81/codeigniter-rest-api/index.php/auth/login:请求标头字段在飞行前响应中,Access-Control-Allow-Headers不允许Access-Control-Allow-Origin。EmployeeLogin.js:2未捕获(承诺)TypeError:无法提取enter image description here

我的网络服务是: 123.0.0.5:81/codeigniter-rest-api/index.php/auth/login

我的React服务器是: localhost / 3000

我已经搜索了解决方案,并尝试在控制器中使用但无法正常工作。

class Auth extends CI_Controller {
    function __construct() {
        parent::__construct();
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");    
    }

我的反应消费脚本是:

(async () => {
          const rawResponse = await fetch('http://123.0.0.5:81/codeigniter-rest-api/index.php/auth/login', {
              method: 'POST',
              headers: {
                  'Accept': 'application/json',
                  'Content-Type': 'application/json'
              },
              body: JSON.stringify(datapost)
          });
          const content = await rawResponse.json();

          alert(content);
      })();

是codeIgniter的新手,我已经使用code-igniter构建了REST Web服务。使用React JS的UI。在邮递员中,Web服务似乎工作正常。但是,当我集成到React JS中时,会抛出错误。 ...

reactjs codeigniter web-services access-control
1个回答
0
投票

fetch('http://更改为fetch('https://fetch('//

© www.soinside.com 2019 - 2024. All rights reserved.