0

so this question ask several time but still i could't figure this out. i'm sending http request from angular app to php service. and i need access the cookies i set from the browser. i know that to access cookies from php service i have to set the withCredentials in my http request. but then again wilcard error raised.

here is the http request

$http({
    url : $scope.urlDomain+ "setting/getAll?skip=0&take=10&orderby=asc",
    method :"POST",
    headers : {
        securityToken : "1124"
    },
    withCredentials: true 
})

i had set the headers in my php file

header("Access-Control-Allow-Origin: * ");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT,POST, GET, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: origin, x-requested-with, content-type, securityToken");

this is the error

XMLHttpRequest cannot load http://localhost/services/getAll?skip=0&take=10&orderby=asc. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute

i stuck in this over day and still could't figure this out. thanks guys

1 Answer 1

2

You cant use header("Access-Control-Allow-Origin: * "); when you enable withCredentials, you have to specify a domain like this:

header("Access-Control-Allow-Origin: 127.0.0.1");
header("Access-Control-Allow-Origin: domain2");
header("Access-Control-Allow-Origin: domain3");
Sign up to request clarification or add additional context in comments.

4 Comments

can i use multiple domains
@IT13122256RanawakaR.A.S.M You will have to add multiple rules, i've updated my answer
thank man. i found a solution to get the host dynamically using this header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}")
@IT13122256RanawakaR.A.S.M It's unsafe to accept every host though, I would make a whitelist and check if the origin is on that list, otherwise deny it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.