0

I have the following angularjs code:

    this.createAccount = function (payload) {

    var parameters = {
        grant_type: 'password',
        client_id: clientId,
        client_secret: clientSecret,
        email: payload.Email,
        password: payload.Password,
        scope: 'myApi offline_access openid'
    }
    return $http({
        method: "POST",
        url: $sce.trustAsResourceUrl(identityApiURL + '/account/register'),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
        },
        data: 'grant_type=' + parameters.grant_type + '&client_id=' + parameters.client_id + '&client_secret=' + parameters.client_secret + '&email=' + encodeURIComponent(parameters.email) + '&password=' + parameters.password + '&scope=' + parameters.scope
    }).error(function (response) {
        $log.error('Something went wrong when creating your account', response);
    });

}

When trying to run it I keep on getting the following error in the console:

Failed to load https://........account/register: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:3000' is therefore not allowed access.

I can see that a config object is being created with my params but data is NULL. Also on the Network I see OPTIONS but no POST method is triggered...

What is wrong?

Thanks in advance!

Vikita
  • 261
  • 6
  • 16
  • [The server you are making the request to has to implement CORS to grant JavaScript from your website access](https://stackoverflow.com/a/23824093/5621827) – jitender Nov 02 '17 at 12:08
  • I was assured it does... I'm making another post for login and it was fine... – Vikita Nov 02 '17 at 12:10
  • error clearly says that your server doesn't respond to preflight request recheck how preflight is enabled on server – jitender Nov 02 '17 at 12:17

1 Answers1

0

It turned out that I had: $http.defaults.headers.common['ApiKey'] setup which was added to the headers by default and prior to my headers object for the POST request. Once I removed it worked.

Vikita
  • 261
  • 6
  • 16