Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

Rest API Codeigniter.

$
0
0
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/codeig...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!

Viewing all articles
Browse latest Browse all 14343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>