1

I make a Get call to my API and want the result as a string so I can split it

  GetServiceProviderId() {
   this.http.get(this.rooturl + 'info', { headers: this.reqHeader });

    var data = "mitId: 18, ServiceProvider: 2" <- result I get and want to split
    var dataspilitted = data.split(" ");
    return dataspilitted[3];
  }
1

3 Answers 3

3

Original guide says you need to specify responseType in request options

GetServiceProviderId() {
    return this.http.get(this.rooturl + 'info', { headers: this.reqHeader, responseType:'text' })
        .map(data => {
    var dataspilitted = data.split(" ");
    return dataspilitted[3];
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Isn't that a JSON response? Why convert to string, split, get the 3rd word, convert to number and hope that nothing changes?

you could just do:

this.http.get(this.rooturl + 'info', { headers: this.reqHeader })
  .subscribe(result => console.log('Result is ', result.ServiceProvider));

Comments

0

You will need to subscribe to the HTTP call and then parse the data from the response depending on how it is formatted.

GetServiceProviderId() {
    this.http.get(this.rooturl + 'info', { headers: this.reqHeader }).subscribe(
        (data) => {
            // Optionally parse "data" into string depending on result format
            // data = parse(data)
            this.dataSplit = data.split(" ")[3];
        }
    );
}

In this example parse(data) is some function that you would write to parse the data into a string, if the HTTP response does not return a string in the first place.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.