No header "Access-Control-Allow-Origin" is present in the requested resource-ion application 2

When trying to access the local server using a POST request, the following error appears:

XMLHttpRequest cannot load http://127.0.0.1:8000/api/v1/users/login . The requested resource is missing the header "Access-Control-Allow-Origin". Therefore, access to the source http: // localhost: 8100 is denied.

My server supports CORS because I checked it by sending a request to the postman and it works.

Here is my code in the interface:

private headers = new Headers({
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
    'Access-Control-Allow-Headers': 'X-Requested-With,content-type',
    'Access-Control-Allow-Credentials': true 
});


postLogin(data) {
    console.log(data);
    return new Promise((resolve) => {
        this.http.post(this.api + "users/login", data, {headers: this.headers})
            .map(res => res.json())
            .subscribe(answer => {
                 this.loggedIn = true;
                 this.token = answer.token;
                 resolve(answer);
            });
    });
 }

PS: I did not get this error with a GET request.

I tried to put a proxy, and this does not change anything :(

This is mine ionic.config.json:

{
  "name": "hardShop",
  "app_id": "",
  "v2": true,
  "typescript": true,
  "proxies": [
      {
          "path": "/api",
          "proxyUrl": "http://127.0.0.1:8000"
      }
  ]
}
+10
3

, .

ajax- , http/https , ( CORS). 'Access-Control-Allow-Origin': '*' , .

, , , . 'Access-Control-Allow-Origin': '*', , *.

, , , , , , . , .

CORS, , , , .

+16

, Http https . , , Https, , .

, , .

, CORS Chrome, .

+1

( ): , connectionstring database.

: ,

const localhost = await mongoose.connect('mongodb://localhost:27017/categories');
const serverhost = await mongoose.connect('mongodb://usename:password@ds015126.mlab.com:12345/myapp');

some may use two connectionStringsjust like me deploy localhost database-connectioninstead of the real one server database-connection, and then try to access databasethrough postmanor client-application, and then run into such a problem.

Be sure you have the deployedright connectionstring.

0
source

All Articles