Search code examples
jsoncordovacordova-pluginsionic4

ionic4 cordova plugin http.get receives json, is expected to deliver json, but delivers string


The service is

import { Injectable } from '@angular/core';
import { HTTP, HTTPResponse } from '@ionic-native/http/ngx';
import { Promise } from 'q';

@Injectable({
    providedIn: 'root'
})
export class YfapiService {
    private baseUrl: string;

    constructor(private http: HTTP) { 
        this.baseUrl = "valid url delivering some json";
    }

    public getDriverDetails(driverKey: string):  Promise<HTTPResponse> {
        return Promise((resolve, reject) => {
            this.http.get(this.baseUrl, {}, {})
            .then(response => resolve(response.data))
            .catch(error => reject(error));
        });
    }
};

url is something valid and delivers json with Content-type being application/json as well. However, in "response" there is "data",and response.data is always a string including thejson received from the remote service. How to turn this into json?

JSON.parse seems to be unavailable. So the second question is: I am doing something completely wrong or did I miss something?

Ionic V4.10.3 Cordova 8.1.2 Plugins installed during the last few days Platform: windows 10, java 8 Platforms set in Cordova: browser, android

Summary:

A.) How to fulfill the promise with response in json B.) If this is not possible, how to turn a string into JSON when JSON.parse() is not available, or how to make JSON.parse() available in my project?


Solution

  • After 2 days of fiddling around, I was disappointed, and therefore closed all the windows, commandlines etc. Finally I killed node.js services in the task manager. A smoke later I started a new attempt. What to tell, JSON was usable now, Promise worked as expected - but both still marked "unknown" by Visual Studio Code. While I thought about this, Visual Studio Code started offering me an update. I went for it.

    What to say: updates help, and sometimes taking a break for both you and your machine helps solving things as well. Maybe this helps a poor soul in the future.