How to unblock the CORS policy

i tried apiurl in post method for signup page…

import { Injectable } from '@angular/core';
import { Http, Headers, HttpModule } from '@angular/http';
import 'rxjs/add/operator/map';

let apiUrl = 'http://aaaaa.in/outfit/index.php/api/v1/';

@Injectable()
export class AuthServiceProvider {

  constructor(public http: Http) {
    console.log('Hello AuthServiceProvider Provider');
  }
  public register(data){
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      alert('11');
        headers.append('Content-Type', 'application/json');
        headers.append('Access-Control-Allow-Origin', '*');
         headers.append( 'Access-Control-Allow-Headers', 'Authorization, Content-Type' );

        //headers.append('Access-Control-Allow-Origin', 'http://localhost:8100');
//headers.append('Access-Control-Allow-Credentials', 'true');

        alert(apiUrl);
        this.http.post(apiUrl+'account', JSON.stringify(data), {headers: headers})
        
          .subscribe(res => {
            resolve(res.json());
           alert(Response);
          }, (err) => {
            reject(err);
          });
    });
  
  }

}

it shows the error like this…“Access to XMLHttpRequest at ‘http://aaaaa.in/outfit/index.php/api/v1/account’ from origin ‘http://localhost:8100’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.”

1 Like

Hello there, i met the same problem… how did you fix that?

1 Like

If I am correct, if you own the server that you are trying to access, you can put smth like

    res.setHeader('Access-Control-Allow-Origin', '*');
	  res.setHeader('Access-Control-Request-Method', '*');
	  res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
	  res.setHeader('Access-Control-Allow-Headers', '*');
//set headers are needed CORS
    res.writeHead(200, {'Content-Type': 'text/html'});
//Your code here

in your code. It should work well now. But if it doesn’t, try using
cors-anywhere.herokuapp.com

which you can used by adding it before your link,
for example, if your link is example.com/?param=blah
you can try to send a request to http://cors-anywhere.herokuapp.com/http://example.com/?param=blah
It works for me tho