Search code examples
angularionic-frameworkionic5

Ionic 5 login api doesn't work, used to work in Ionic 3


Hello i am trying to migrate from ionic 3 to ionic 5. My login function works fine in Ionic 3 but i am getting error on Ionic 5. Php function works fine i 've checked it. The thing is that it does not get inside if(data.success). loginData credentials are shown ok in console.log

  login() {
    this.userData.login(this.loginData).pipe(
        map((data:any) => {
         console.log(this.loginData)//this.loginData is ok
          if (data.success) {
           console.log('data is sent');//this is NOT shown on console
          }
          else {
            alert('data is not sent');//this is shown on console
            }
          })
        ).subscribe()
      }

userdata.login function

   this.options = new HttpHeaders({
  'Content-Type': 'application/x-www-form-urlencoded'
 });

login(login_data) {
    const url = this.appData.getApiUrl() + 'login';
    const data = this.jsonToURLEncoded({
        login_data: login_data,
        app_version: this.appData.APP_VERSION
    });
    console.log('here');//this shows
    return this.http.post(url, data, this.options);
    console.log('here2')//unreachable code

jsonToURLEncoded function

 jsonToURLEncoded(obj) { // this is ok, you need to still add some code in the api, but for apple is just to be available in the app ok got what this does is send a notificaion on phone to say what you want to say api wise like simon created a new media? correctok so
var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
for (name in obj) {
  value = obj[name];

  if (value instanceof Array) {
    for (i = 0; i < value.length; ++i) {
      subValue = value[i];
      fullSubName = name + '[' + i + ']';
      innerObj = {};
      innerObj[fullSubName] = subValue;
      query += this.jsonToURLEncoded(innerObj) + '&';
    }
  }
  else if (value instanceof Object) {
    for (subName in value) {
      subValue = value[subName];
      fullSubName = name + '[' + subName + ']';
      innerObj = {};
      innerObj[fullSubName] = subValue;
      query += this.jsonToURLEncoded(innerObj) + '&';
    }
  }
  else if (value !== undefined && value !== null)
    query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}//api is one on server phph slim is

return query.length ? query.substr(0, query.length - 1) : query;
}

i think the problem is with this.options any help?


Solution

  • You have to declare headers

    from

    return this.http.post(url, data, this.options);
    

    to

     return this.http.post(url, data, {headers: this.options});