Ionic2: Http sends {"isTrusted": true} - Conflict with other observables?

I am trying to develop a new application with Ionic2. In this context, the user must be authenticated with a pre-shared token, which will be sent to the api (http request). In response, he receives an access token, with which he can use additional api functions.

The problem is that when I send an HTTP request, I get an error: {"_body": {"isTrusted": true}, "status": 200, "its status": "Ok", "headers": {} , "type": 3, "URL": NULL}

Everything works fine on my desktop version, but on Android I get this error. I was still looking for this error, and I assume that the HTTP request is in conflict with other observables (Form observable).

I would be very pleased if someone helped me avoid this “mistake." As a small hint: {"isTrusted": true} - the value of the handleSubmit method event value. Thank: -)

Solution: This error was caused because the api could not be reached (in my case, due to a certificate error). If you have the same error: Check if type = "3", then it is possible that your HTTP landing page is also not available.

My template:

<ion-content padding class="token-auth">
  <form [ngFormModel]="authForm" (submit)="handleSubmit($event)">
    <ion-item>
      <ion-label floating>Device-ID</ion-label>
      <ion-input (keyup)="onKeyUp($event)" [ngFormControl]="token" type="text"></ion-input>
    </ion-item>

    <div *ngIf="error !== null"
         class="error-box">
      {{error.message}}
    </div>

    <button block type="submit" [disabled]="!authForm.valid">
      Register device
    </button>

  </form>
</ion-content>

Page Source:

handleSubmit(event) : void {          
  this.tokenService.sendAccessTokenRequest(this.authForm.value.token, (err, code) => {
        this.error = {
            code : code,
            message : err
        };
    }, (accessToken, refreshToken, validTill) => {
        this.error = null;
    });
}

TokenService:

/**
 *
 * @param token
 * @param errCallback : (message : string, code : number) => any
 * @param callback : (accessToken : string, refreshToken : string, validTill : string) => any
 */
public sendAccessTokenRequest(token : string, errCallback, callback) {
    if (typeof token === 'undefined') {
        throw 'Token value must be set. ';
    }
    if (typeof errCallback === 'undefined') {
        errCallback = (code, message) => null;
    }
    if (typeof callback === 'undefined') {
        callback = (accessToken, refreshToken, validTill) => null;
    }

    console.log(`Should send token: ${token}`);

    this.sendPost(`/auth/login`, `application=XXX&device_token=${token}`)
        .subscribe(res => {
            console.log("Device verified: "+JSON.stringify(res));
            let {
                access_token,
                refresh_token,
                valid_till
            } = res.data.token;
            this._saveTokenData(access_token, refresh_token, new Date(valid_till));
            callback(access_token, refresh_token, valid_till);
        },
        err => {
            // error is here!
            if (err && err._body) {
                console.log("ERROR: "+JSON.stringify(err));
                try {
                    let bodyData = JSON.parse(err._body);
                    let responseData = bodyData["data"];
                    let token:MessageResponse = responseData.token;
                    if (token && typeof token.code !== 'undefined' && typeof token.userMessage !== 'undefined') {
                        errCallback(token.userMessage, token.code);
                    } else {
                        errCallback(bodyData);
                    }
                } catch (e) {
                    errCallback(e);
                }
            }
        },
        () => console.log("access token request successful")
    );
}

// just an excerpt from other url
sendPost(link : string, params : string, header : Object = {}) {
    console.log("SEND POST");
    link = this._enforceSlashAtStringStart(link);
    return this.http.post(`${this.base_url}${link}`, params, {
        headers : this._mergeHeader(this.getHeader(), header)
    }).map(res => { console.log("Start mapping! "); return res.json(); });
}

// EDIT: added function _saveTokenData
private _saveTokenData(accessToken : string, refreshToken : string, validTill : Date) {
        if (!accessToken || !refreshToken || !validTill) {
            throw new Error('access token data can not be null or undefined! ');
        }

        console.log("TOKEN SAVED!");

        this._accessToken = accessToken;
        this._refreshToken = refreshToken;
        this._validTill = validTill;

        // this._storage = new Storage(LocalStorage);
        this._storage.set(TOKEN_STORAGE_KEY, JSON.stringify({
            access_token : accessToken,
            refresh_token : refreshToken,
            valid_till : validTill.toString()
        }));
    }

And last but not least: magazines:

14    256534   log      Should send token: XXXXXXXXXXXXXX
15    256548   log      SEND POST
16    256750   log      ERROR: {"_body":{"isTrusted":true},"status":200,"statusText":"Ok","headers":{},"type":3,"url":null}
+4
1

webkit ios .

-1

All Articles