Cors Issue In Ionic View


#1

Hello i have deployed my project in ionic view where i need to make a http call to remote server.I have implemented proxies like this

{
 "name": "MobileUI",
 "app_id": "608c237d",
 "type": "ionic-angular",
 "proxies": [
   {
    "path": "/Auth",
    "proxyUrl": "https://example.com/Auth/Authenticate",
     "rejectUnauthorized": false
    }
  ]
}

And I make a login call to the from the provider like this

  AuthenticateUser(username: string, password: string) {
var userNamePwd: any = {};
userNamePwd.username = username;
userNamePwd.password = password;
let body = JSON.stringify(userNamePwd);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post('/Auth', body, options)
  .map((res) => {
    let data = this.extractData(res);
    this.storage.set('id_token', data[0].Token);
    this.sharedAuth.SetEntityData(data);
    return data;
  })
  .catch(this.handleError);
 }

The Login works perfectly when testing in web browser but when i upload the app in my ionic view i am getting this error response with status 0 for url null
I implemented the proxies in the way mentioned in http://blog.ionic.io/handling-cors-issues-in-ionic/ .
Am I missing something ?


#2

Service Proxies are a CLI feature. They are only available for local testing, not in Ionic View or other apps. /Auth won’t be available in Ionic View at all.


#3

Thank you for the reply.I used to make the server call with out proxies like this

  AuthenticateUser(username: string, password: string) {
    var userNamePwd: any = {};
    userNamePwd.username = username;
    userNamePwd.password = password;
    let body = JSON.stringify(userNamePwd);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.post('https://example.com/Auth/Authenticate', body, options)
      .map((res) => {
        let data = this.extractData(res);
        this.storage.set('id_token', data[0].Token);
        this.sharedAuth.SetEntityData(data);
        return data;
      })
      .catch(this.handleError);
  }

Even this doesn’t work in ionic view.How can i solve the cors issue in ionic view.Can you help please


#4

CORS is solvable with headers on the server side. The error messages are quite specific what has to be changed to get it working.


#5

sorry i didn’t understand.Can you please tell me how can i solve this issue.My web application works fine without changing anything on the server side.Is it possible to solve this error on the client side only without touching the server side?


#6

No.

Read about CORS here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS


#7

Ok thank you :slight_smile: