Access-Control-Allow-Origin does not appear in response headers from codeigniter

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 I get is:

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 is Access-Control-Allow-Originmissing from the response even after inclusion Access-Control-Expose-Headers: Access-Control-Allow-Origin. My source for this title is Mozilla Developer Website

+3
source share
4 answers

Turns out it only worked for me when I set the headers via PHP header()syntax instead of codeigniter syntax $CI->output->set_header(). It is sad.

Thanks to @Yan's first comment in the Question of this section

+5
source

, , : it text/html, application/json. , , , . , :

  • set_output, .

    $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);
    
  • -display() , , json- .

    $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"));
    

    . ( CI/system/core/Output.php 316)

+3

, $CI- > output- > set_header() - , .

, CI, , include(VIEWPATH.'errors/'.$template.'.php') , set_status_header($status_code) ( <CI System Dir>/core/Common.php)

. <CI System Dir>/core/Exceptions.php

:

    /**
     * General Error Page
     *
     * Takes an error message as input (either as a string or an array)
     * and displays it using the specified template.
     *
     * @param   string      $heading    Page heading
     * @param   string|string[] $message    Error message
     * @param   string      $template   Template name
     * @param   int     $status_code    (default: 500)
     *
     * @return  string  Error page output
     */
    public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        set_status_header($status_code);

        $message = '<p>'.implode('</p><p>', is_array($message) ? $message : array($message)).'</p>';

        if (ob_get_level() > $this->ob_level + 1)
        {
            ob_end_flush();
        }
        ob_start();
        include(VIEWPATH.'errors/'.$template.'.php');
        $buffer = ob_get_contents();
        ob_end_clean();
        return $buffer;
    }

, DRY . , , ():

function my_generate_headers($headers=array(),$useOutputClass=true)
{
    if(is_array($headers) && count($headers)<1) return false;

    foreach($headers AS $eHeader)
    {
        ($useOutputClass) ? 
                    get_instance()->output->set_header('X-Powered-By: C-C-C-Cocaine') : 
                    @header('X-Powered-By: Errors',true);
    }

    return true;
}

<CI Views>/errors/error_*.php, .

0
source

What worked for me:

$this->output
  ->set_header('Access-Control-Allow-Origin: http://localhost:4567')
  ->set_header('Content-type: application/json')
  ->set_status_header(200)
  ->set_output( json_encode($to_encode) )
  ->_display();
0
source

All Articles