Access-Control-Allow-Origin is not showing up in response headers from codeigniter

10,047

Solution 1

It turns out, it worked for me only when i set the headers via the PHP syntax header() instead of the codeigniter syntax $CI->output->set_header(). That's sad.

Thanks to the first comment by @Yan at the Question of this topic

Solution 2

If you look closely you can also notice the content-type being different: it's text/html, whereas you are requesting application/json. This happens because while you are preparing the headers correctly, you never actually output them. As far as I know you can do this in at least 2 ways:

  1. Use the output library's set_output function to output everything at once.

    $json = json_encode(array("city" => "dhaka"));
    
    $this->output->set_header("Access-Control-Allow-Origin: *");
    $this->output->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
    $this->output->set_status_header(200);
    $this->output->set_content_type('application/json');
    
    $this->output->set_output($json);
    
  2. Call the output-library's _display() function, to first output the correct headers and then append your json object with echo.

    $this->output->set_header("Access-Control-Allow-Origin: *");
    $this->output->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
    $this->output->set_status_header(200);
    $this->output->set_content_type('application/json');
    $this->output->_display();
    
    echo json_encode(array("city" => "dhaka"));
    

    This function sends the finalized output data to the browser along with any server headers and profile data. (From CI/system/core/Output.php line 316)

Share:
10,047
Rakib
Author by

Rakib

DevOps engineer, web & mobile app backend engineer, cloud computing solutions architect, tech trainer for multiple tech startups & enterprises specializing in CloudInfra, RESTful APIs, Microservices, ETL.

Updated on June 27, 2022

Comments

  • Rakib
    Rakib almost 2 years

    My Codeigniter file says

    $CI->output->set_header("Access-Control-Allow-Origin: *");
    $CI->output->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
    $CI->output->set_status_header(200);
    $CI->output->set_content_type('application/json');
    echo json_encode(array("city" => "dhaka"));
    

    but the http response that i get are:

    Request URL:http://localhost/index.php/location/city
    Request Method:POST
    Status Code:200 OK
    
    Connection:Keep-Alive
    Content-Length:16
    Content-Type:text/html
    Date:Sun, 22 Jul 2012 10:27:32 GMT
    Keep-Alive:timeout=5, max=100
    Server:Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.6
    X-Powered-By:PHP/5.3.6
    

    The header Access-Control-Allow-Origin is missing in the response even after including Access-Control-Expose-Headers: Access-Control-Allow-Origin. My source of information about this header is from Mozilla Developer Website