CodeIgniter Forums
Rest API Codeigniter. CORS PROBLEM [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Rest API Codeigniter. CORS PROBLEM [SOLVED] (/showthread.php?tid=70476)



Rest API Codeigniter. CORS PROBLEM [SOLVED] - theedo - 04-15-2018

Hello, I'm working on ReactJS. I'm using React for the front-end part of website, and as backend I'm using Codeigniter with the RestAPI library (https://github.com/chriskacerguis/codeigniter-restserver). 

I'm working on http://localhost:8080 and PHP is on a link (created in hosts file) http://spotalefinale.com.

Now, I've a controller:
PHP Code:
<?php
/**
 * Created by PhpStorm.
 * Date: 15/04/2018
 * Time: 18:26
 */

require APPPATH 'libraries/REST_Controller.php';

class 
Login extends REST_Controller
{

 
   public function __construct($config 'rest')
 
   {
 
       parent::__construct($config);
 
       header('Access-Control-Allow-Origin: *');
 
       header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
 
       header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
 
       $method $_SERVER['REQUEST_METHOD'];
 
       if($method == "OPTIONS") {
 
           die();
 
       }

 
   }

 
   public function index_post(){
 
       header('Access-Control-Allow-Origin: *');
 
       header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
 
       header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
 
       $method $_SERVER['REQUEST_METHOD'];
 
       if($method == "OPTIONS") {
 
           die();
 
       }
 
       echo json_encode(array("hey" => "hey!"));
 
   }

 
   public function index_options() {
 
       return $this->response(NULLREST_Controller::HTTP_OK);
 
   }



and a sort of jquery ajax request:
Code:
handleSubmitLogin(e){
       e.preventDefault();
       var data = {
           nameLogin: this.state.loginName,
           passwordLogin: this.state.loginPassword
       };
       axios.post(url + 'a', data).then(function(res){
           console.log(res);
       })
       .catch(function(err){
           console.log(err);
       });
   }

(axios works as $.ajax). 

I'm getting this error: "Failed to load http://spotalefinale.com/a: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access." but I enabled the cors in the controller, as you can see. Now if I enable the CORS with a Chrome extension, it works without any problem. (This is the extension). 

Which is the problem? Why without enabling the extension the cors doesn't work? Thank you if you have some ideas!


RE: Rest API Codeigniter. - InsiteFX - 04-15-2018

Here is a very good article on it.

Cross-Origin Resource Sharing (CORS)

Hope that helps.


RE: Rest API Codeigniter. - theedo - 04-15-2018

Yeah, thank you. I read it, but it doesn't help me too much.

I don't know if it is a "local" problem (I'm running on localhost:8080), maybe is it?


RE: Rest API Codeigniter. - InsiteFX - 04-15-2018

Did you try using a vhost setup?

Also did you see if Chrome Developer is showing any errors?


RE: Rest API Codeigniter. - theedo - 04-15-2018

Ok, I tried to host the frontend (reactjs) on a server with an accessible domain, then I tried one request and it doesn't work.
I've always this error: [Image: 9f1901669320d69ae4bec007a24a76ac.png]

UPDATE: SOLVED!

So, I was using IIS, so I added to my web.config:

Code:
<httpProtocol>
 <customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Content-Type" />
   <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
 </customHeaders>
</httpProtocol>
then I changed my controller to that
PHP Code:
<?php
/**
 * Created by PhpStorm.
 * User: Edoardo
 * Date: 15/04/2018
 * Time: 18:26
 */

require APPPATH 'libraries/REST_Controller.php';

class 
Login extends REST_Controller
{

 
   public function __construct($config 'rest')
 
   {
 
       parent::__construct($config);


 
   }

 
   public function index_post(){
 
       echo json_encode(array("hey" => "hey!"));
 
   }

 
   public function index_options() {
 
       return $this->response(NULLREST_Controller::HTTP_OK);
 
   }



and now it works!


RE: Rest API Codeigniter. CORS PROBLEM [SOLVED] - InsiteFX - 04-16-2018

Glad to hear you got it to work.

Please edit your title and add this at the end of it [SOLVED]

Enjoy