I encountered an issue with my API where I made three Get requests using the same function but different URLs to differentiate between them. However, even though the provider returns the data in steps, the page response function does not receive it and shows either null or "Unexpected end of JSON".
Page Code:
loadNature() {
this.expenseProvider.fetchDropdown('http://xx.xx.xx.xx:xxxx/api/v1/cadastros/natureza/get-dropdownlist/002/001')
.then((arr_proj) => {
this.obj_data = arr_proj;
this.nature_list = [];
for (let i = 0; i < this.nature_list.length; i++) {
this.obj += (
this.nature_list[i]
)
this.nature_list.push(this.obj);
}
})
}
Provider code:
fetchDropdown(url: string) {
let headers = new Headers();
headers.append('Content-Type', 'application/json; charset=UTF-8');
headers.append('Authorization', 'bearer ' + this.global.tokenGlobal);
let options = new RequestOptions({ headers: headers });
return this.http.get(url, options)
.toPromise()
.then(response => {
var json_data = (response as any)._body;
var parsed = JSON.parse(json_data);
var arr_data = [];
for (var x in parsed) {
arr_data.push(parsed[x]);
}
return arr_data;
})
.catch((error) => {
var json_error = (error as any)._body;
var parsed = JSON.parse(json_error);
var arr = [];
for (var x in parsed) {
arr.push(parsed[x]);
}
return arr[0];
});
}