2

I am trying to work with AJAX request in my codeigniter app. At the end of my codeigniter controller function, I added

public somefunction(){

 $this->output->set_header('Access-Control-Allow-Origin: *');
 $this->output->set_header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
 $this->output->set_content_type('application/json');
// plan contains array
 return $this->output->set_output(json_encode($plan));
}

Normal get request works via server to server, but AJax calls shows the error. XMLHttpRequest cannot load localhost:8888. Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

Here is the ajax calls

self.information = function() {
      $.ajax({
          type: 'GET',
          url: '',
          contentType: 'application/json; charset=utf-8'
      })
      .done(function(result) {
        console.log(result);
      })
      .fail(function(xhr, status, error) {
          alert(error);
      })
      .always(function(data){
      });
  }

The url works, since I checked it with postman and I get data returned. So no problem with that.

7

2 Answers 2

3

I came across the same issue today, what worked for me was defining the index_options method

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

and updating my the constructor to this

public 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");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
}

Unfortunately I'm not familiar enough with access control to explain why this worked for me. Hope this helps.

5
  • Hello Awad, so do I add it to my somefunction() ??? How does the whole thing work together?
    – XAF
    Feb 28, 2017 at 7:48
  • @Farhana just define the index_options function just like your somefunction function, that will stop the browser from complaining about the preflight OPTIONS request. Next, move the header modifications to the constructor Feb 28, 2017 at 8:52
  • the index_options function is not called anyhow, how will it load? Or the ci automatically loads it ?
    – XAF
    Feb 28, 2017 at 9:34
  • Yes, if I'm not mistaken its the browser that calls the index_options function Feb 28, 2017 at 9:43
  • @AwadMaharoof Saved the day! Aug 31, 2023 at 12:39
0

I recommend to you to use RestServer for REST API which has CORS managing in it's settings. Just set some settings in rest.php file in application/config folder:

$config['check_cors'] = true; //Set to TRUE to enable Cross-Origin Resource Sharing (CORS).
$config['allowed_cors_headers'] = ['Header1','Header2'...] //If using CORS checks, set the allowable headers here
$config['allowed_cors_methods'] = ['GET','POST','OPTIONS',...] //If using CORS checks, you can set the methods you want to be allowed
$config['allow_any_cors_domain'] = true; //Set to TRUE to enable Cross-Origin Resource Sharing (CORS) from any source domain
$config['allowed_cors_origins'] = []; //specify what origins is allowed ($config['allow_any_cors_domain'] must be false)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.