When i use var datos = JSON.parse(this.dato); in ionViewDidLoad() i have this error "Unexpected end of JSON input"

Hi, i need load the response of the query in the diferent elemento.innerHTML. If i do this when push a button witch function “cargar()” it works, but if i want to that this function run in ionViewDidLoad() i have this error “Unexpected end of JSON input”.
the same happens if i cut the code of “cargar()” and paste it in “ionViewDidLoad()”.

i have this code:

ionViewDidLoad() {

 this.cargar();

}

cargar(){

var link = 'http://dominio.ml/api.php';
var flag = 5;
var myData = JSON.stringify({id: this.id, flag: flag});

this.http.post(link, myData)
.subscribe(data => {
this.data.response = data["_body"]; 
this.dato = this.data.response;
var datos = JSON.parse(this.dato);

for (var i in datos) {

	var elemento = document.querySelector('#button'+i);
	elemento.innerHTML = datos[i].usuario;	
	
}

});

}

This code is extremely unidiomatic.

  • Components should not be interacting with Http directly; use a service provider to do that.
  • You should not be manually stringifying HTTP POST bodies
  • You should never access underscore-prefixed members of other objects; they are private
  • You should instead call methods such as json() on the response object
  • You should not ever access the DOM manually. If you’re writing document in an Ionic app, you’re doing it wrong. Use property bindings instead.
  • innerHTML only works for very primitive content. If you need anything beyond basic text and bold/italic, you’re going to be disappointed and should rethink the design.

this.data.response = data.json() :wink:

thanks for your comments, I’m really learning.
I will read the information on the topics that describe me.