No 'Access-Control-Allow-Origin' header is present on the requested resource - ionic 2 application

I receive the following error when i try to access my local server with a POST request:

XMLHttpRequest cannot load http://127.0.0.1:8000/api/v1/users/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

My server allows CORS because I've tested it by sending the request with postman and it works.

Here is my code in the front-end:

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 it doesn't change anything :(

This is my ionic.config.json:

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

Try again using this plugin in your browser.

It allows you to ajax request any address from any source, bypassing http/https security requirements or other limitations set by browsers (known as CORS). Practically it injects the 'Access-Control-Allow-Origin': '*' header in the received response before it is passed on to your app.

Please keep in mind that this is a band-aid solution, predominantly for development. Your server's response has to actually have the 'Access-Control-Allow-Origin': '*' header, preferably with a more specific value than *.

What you are doing now has practically no effect, since you as the client are sending a request with that header to the server, who then promptly ignores it. What matters is that the server has this header in his response to your client.

Postman doesn't apply CORS as far as i know, so maybe that's why it's not affected.

No, No definition, (a negative used to express dissent, denial, or refusal, as in response to a question or request) See more. No definition, (a negative used to express dissent, denial, or refusal, as in response to a question or request) See more.

This errors shows when you are connecting a Http with a https i.e a secured layer. I got the same error when I was calling a rest service that was in a Secured server with Https to my application running in localhost.

To allow that, you need to install a plugin in your browser which would pass this lock.

If you are using chrome, install CORS plugin for chrome and it will work.

No (Meghan Trainor song), Examples of no in a Sentence. Adverb She shook her head no. this cake is no better than the last one we made. No is used in front of nouns to mean 'not any'. For example, instead of saying 'She doesn't have any friends', you can say 'She has no friends '. I have no complaints. My children are hungry.

One reason of this problem can be (personal experience): May be you have deployed the wrong connectionstring for database.

I was using the following two database connections, one for local storage and other for server storage

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

some may be using two connectionStrings just like me and deploy the localhost database-connection instead of real server database-connection and then try to access the database via postman or client-application then face such problem.

Be sure, you have deployed the right connectionstring.

No, Not (a); not properly, not really; not fully. My mother's no fool. Working nine to five every day is no life. Derived terms� My name is no, my sign is no, my number is no, uh You need to let it go, uh You need to let it go, uh Need to let it go, uh Nah to the ah to the, no, no, no My name is no, my sign is no, my number

no, Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking� no. or No. abbr. 1. a. north b. northern 2. number No. abbreviation for pl Nos or nos 1. (Physical Geography) north(ern) 2. Also: no number [from French numéro] ThesaurusAntonymsRelated WordsSynonymsLegend: Switch to new thesaurus Noun 1. no. - the number designating place in an ordered sequence ordinal, ordinal number number - a concept of quantity

Google, Despite Pentagon statements that it disbanded a once-covert program to investigate unidentified flying objects, the effort remains underway� 31 synonyms of no from the Merriam-Webster Thesaurus, plus 44 related words, definitions, and antonyms. Find another word for no. No: not in any degree, way, or under any condition.

No Longer in Shadows, Pentagon's U.F.O. Unit Will Make Some , 1.1Used to indicate that something is quite the opposite of what is being specified . 'it was no easy task persuading her'. More example sentences. This feature is not available right now. Please try again later.

Comments
  • This looks like your server does not support CORS. Are you sure you implemented it correctly? I think postman does not have any issues with CORS, so just because it works in Postman does not mean that your server supports CORS. Try looking at the response in your Ionic app if there are any CORS related headers present.
  • In it's current state this should be a comment. You should explain what this add-on is and how it can help solve the problem.
  • I agree with @rottenoats. Also, this does not solve the problem if he wants to deploy the app as a webapp or if he is using WkWebView on iOS, which enforces CORS even on a device.
  • Your link to plugin is broken of may be plugin is removed.