Php 访问控制允许源代码未显示在codeigniter的响应标头中

Php 访问控制允许源代码未显示在codeigniter的响应标头中,php,ajax,codeigniter,xmlhttprequest,Php,Ajax,Codeigniter,Xmlhttprequest,我的Codeigniter文件说 $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_t

我的Codeigniter文件说

$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"));
但我得到的http响应是:

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

响应中缺少标题
Access Control Allow Origin
,即使在包含
Access Control Expose Headers:Access Control Allow Origin
之后也是如此。我关于此标题的信息来源于

如果仔细查看,您也会注意到内容类型不同:它是
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()函数,首先输出正确的头,然后用echo附加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行)


  • 事实证明,只有当我通过PHP语法
    header()
    而不是codeigniter语法
    $CI->output->set_header()
    设置头时,它才对我有效。那太可悲了


    多亏了这个主题的问题

    在深入研究之后,我发现$CI->output->set_header()在没有错误或异常的情况下可以正常工作

    当CI可以捕获错误或异常时,将完全绕过输出和视图类,并使用
    include(VIEWPATH.errors/'.$template.'.php')
    呈现相应的错误页面,并使用
    set\u status\u header($status\u code)
    发送头(位于
    /core/Common.php

    请参见
    /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;
        }
    
    /**
    *一般错误页
    *
    *将错误消息作为输入(字符串或数组)
    *并使用指定的模板显示它。
    *
    *@param string$标题页标题
    *@param string | string[]$消息错误消息
    *@param string$模板模板名称
    *@param int$状态代码(默认值:500)
    *
    *@返回字符串错误页面输出
    */
    公共函数show_error($heading,$message,$template='error_general',$status_code=500)
    {
    设置_status_头($status_code);
    $message=“”。内爆(“

    ”,是数组($message)?$message:array($message))。

    ”; 如果(ob_get_level()>$this->ob_level+1) { ob_end_flush(); } ob_start(); 包括(VIEWPATH.errors/.$template..php'); $buffer=ob_get_contents(); ob_end_clean(); 返回$buffer; }
    这让人恼火,因为它会让干涸变得不那么笔直。为了解决这个问题,我建议您创建一个助手函数,例如(未测试):

    函数my_generate_头($headers=array(),$useOutputClass=true)
    {
    如果(is_数组($headers)&&count($headers)输出->设置_头('X-Powered-By:C-C-C-C'):
    @标题('X-Powered-By:Errors',true);
    }
    返回true;
    }
    
    /errors/error.*.php的不同错误页面以及控制器中使用该功能。

    对我有效的是:

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

    header('Access-Control-Expose-Headers:Access-Control-Allow-Origin')
    work?这很奇怪…….它刚刚工作了。Codeigniter输出类没有呈现它?在stackoverflow上发布了这个ISSUE之后,我一直在关注您刚才在选项1中发布的内容。它仍然不工作。事实证明,只有当我通过PHP语法
    header()设置头时,它才对我有效
    而不是codeigniter语法
    $CI->output->set_header()
    。这太可悲了。
    $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();