4

I am getting an error like:

origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 

This error occurs only when the design is integrated in codeigniter.

Please note who all added this question as duplicate. my question is not a duplicate question of this . My solution is added as the comment. Since, some geeks who think that, its a duplicate question, and reviewd it like so, I am not able to answer my own question. Once I get the privilage to add so, I will move the comment to answer.

Thankyou

Raru
  • 371
  • 1
  • 4
  • 19
  • 1
    What does this has to do with css? – Renzo Calla Sep 26 '17 at 12:31
  • 1
    In short, you must config your server to accept CORS requests. More information. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin – AVAVT Sep 26 '17 at 12:31
  • 1
    check if you have some request to i.e. `http://localhost/...` css or js file – vnt Sep 26 '17 at 12:33
  • 1
    @AVAVT, I think it's incorrect to say "you must config your server to *accept* CORS requests". It's the opposite. The server Raru is attempting to connect to needs to include CORS header(s) in it's *response*. – Raphael Rafatpanah Sep 26 '17 at 12:35
  • @RaphaelRafatpanah you're right, I didn't notice that until now o_0 – AVAVT Sep 26 '17 at 12:37
  • 2
    @AVAVT, another useful piece of information: The browser is what is restricting the connection. If no CORS header is present in the Response, the browser won't connect (maybe not all browsers though). However, since no CORS headers are present at all, you *would* be able to connect to that origin from another server, *unless* the HTTP Response specifically whitelists certain origins excluding yours / blacklists your origin. The browsers are doing this to prevent potentially malicious documents. https://stackoverflow.com/questions/9222822/why-do-browser-apis-restrict-cross-domain-requests – Raphael Rafatpanah Sep 26 '17 at 12:46
  • 1
    I got the answer. In application configuration I had change like this. $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/rvci/'; and this worked. – Raru Oct 06 '17 at 04:58

1 Answers1

1

You need to add headers in Your requested page:

  // headers
  header("Access-Control-Allow-Origin: *");
  header("Access-Control-Allow-Methods: POST");
  header("Access-Control-Allow-Headers: Origin, Methods, Content-Type");

EDIT

in codeigniter:

$this->output
        ->set_content_type("Access-Control-Allow-Origin: *")
        ->set_content_type("Access-Control-Allow-Methods: POST")
        ->set_content_type("Access-Control-Allow-Headers: Origin, Methods, Content-Type")
Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
  • I am really sorry... It didn't make any changes. – Raru Sep 26 '17 at 12:32
  • 2
    @Raru, these headers need to be included in the HTTP Response of the server your trying to connect to. If you don't have control of the that server, then you won't be able to connect via the browser. You will, however, be able to connect from another server. The browser is what is restricting you here. – Raphael Rafatpanah Sep 26 '17 at 12:38