Asked  7 Months ago    Answers:  5   Viewed   36 times

http.get request in agularJs controller works fine when my client app and api are in localhost. when api is moved to server., issue arised.

client side using angularJs

$http.get('http://domain.com/api/spots/2/0').success(function(datas){
console.log(datas);
});

log gives: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://domain.com/api/spots/2/0. This can be fixed by moving the resource to the same domain or enabling CORS.

i have added these two lines to my controller construct

header("Access-Control-Allow-Origin: *");

header("Access-Control-Allow-Methods: GET");

still same error.

 Answers

17

Try adding OPTIONS to the allowed methods.

header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");

and return immediately when the request is method 'OPTIONS' once you have set the headers.

if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
    die();
}

See also this answer.

Angular sends a W3C CORS spec compliant preflight request that will check for the right allowed methods before actually attempting it.

Personally, I find the Mozilla Developer Network CORS page a bit easier to read on the matter to help understand the flow of CORS.

Wednesday, March 31, 2021
 
AntoineB
answered 7 Months ago
38

Change folder name CodeIgniter-3.1.6 to ci

Set your base_url to

$config['base_url'] = 'http://localhost/ci/

Use this .htaccess

RewriteEngine On
RewriteBase /ci
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Saturday, May 29, 2021
 
superfell
answered 5 Months ago
95

For htaccess I suggest:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)?*$ index.php/$1 [L,QSA]

And know that when you kill session CI will create a new 1 on your next Page load (in your case on the redirect) .. Session isn't just for user logins.

Don't forget at app/config/config.php to set

$config["index_page"] = '';
Saturday, May 29, 2021
 
BradM
answered 5 Months ago
14

There are two sample functions provided by the Firebase team that demonstrate the use of CORS:

  • Time server with date formatting
  • HTTPS endpoint requiring Authentication

The second sample uses a different way of working with cors than you're currently using.

Consider importing like this, as shown in the samples:

const cors = require('cors')({origin: true});

And the general form of your function will be like this:

exports.fn = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
        // your function body here - use the provided req and res from cors
    })
});
Tuesday, June 1, 2021
 
shin
answered 5 Months ago
27

Fixed by adding --cors-allowed-origins=["http://*"] argument to /etc/default/kube-apiserver file. Then restarted to kube-apiserver.

Tuesday, October 19, 2021
 
Igor Tupitsyn
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :