close

[Solved] Codeigniter CORS policy: No ‘Access-Control-Allow-Origin’ error How to resolve?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Codeigniter CORS policy: No ‘Access-Control-Allow-Origin’ error How to resolve? in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Codeigniter CORS policy: No ‘Access-Control-Allow-Origin’ error How to resolve Error Occurs?

Today I get the following error Codeigniter CORS policy: No ‘Access-Control-Allow-Origin’ error How to resolve? in php.

How To Solve Codeigniter CORS policy: No ‘Access-Control-Allow-Origin’ error How to resolve Error ?

  1. How To Solve Codeigniter CORS policy: No 'Access-Control-Allow-Origin' error How to resolve Error ?

    To Solve Codeigniter CORS policy: No 'Access-Control-Allow-Origin' error How to resolve Error Codeigniter is a cool framework to manipulate PHP, for CORS, you don't need to enable it as it has security implications, just do the following

  2. Codeigniter CORS policy: No 'Access-Control-Allow-Origin' error How to resolve?

    To Solve Codeigniter CORS policy: No 'Access-Control-Allow-Origin' error How to resolve Error Codeigniter is a cool framework to manipulate PHP, for CORS, you don't need to enable it as it has security implications, just do the following

Solution 1

Use header() functions in your Codeigniter __construct

    public function __construct()
            {
                parent::__construct();
                $this->load->model('api_model');
                $this->load->library('form_validation');
        
                Header('Access-Control-Allow-Origin: *'); //for allow any domain, insecure
                Header('Access-Control-Allow-Headers: *'); //for allow any headers, insecure
                Header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); //method allowed

//Or

        header('Access-Control-Allow-Origin: website_url');
        header("Content-Type: application/json; charset=UTF-8");
        Header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); //method allowed

Solution 2

Codeigniter is a cool framework to manipulate PHP, for CORS, you don’t need to enable it as it has security implications, just do the following

  1. open config.php,
  2. look for $config['base_url'] = "";
  3. change it to $config['base_url']="http://localhost/youproject/";

Save and reload your application. Your good to go

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment