Error while trying to post data into my local server


#1

I created a sample registration form and tried to insert data into local database ( am using php for server side ) but i got the following error

XMLHttpRequest cannot load http://localhost/manager/managerApi.php?key=register. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

ERROR Error: Uncaught (in promise): Response with status: 0  for URL: null

Find below my code that i wrote in the provider :

postData(data,key) {



    return new Promise((resolve,reject) => {

      let headers = new Headers();
      this.http.post(baseUrl+key,JSON.stringify(data),{headers:headers}).subscribe(res => {
        resolve(res.json());

      },(err) => {

          reject(err);

      });

    }); 

  }


#2

The error message is quite clear: Your response needs that header.

Alternative: https://github.com/ionic-team/ionic-cli#service-proxies


#3

Yes, that was the issue. I found it. Anyway thanks for your help


#4

So how did you fix it?


#5

Added these in my api

    header('Access-Control-Allow-Origin: *');
	header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
	header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");