Retrieve a json web service datuum

Hello,

The more I’m working on the consumption of my web service, the more I’m getting lost…
I run my Ionic project onto the google chrome browser, I had to install the cors plugin on my browser to run my web service,it works when I use the ionic cordova run browser line command (also, I found a bug by using this command line, I published an issue, I hope it will be solved very soon).

So, I’m having a huge problem I can’t understand, it’s with this code :

let ws_function="DonnerConnexion";
	let ws_param="aLogin="+encodeURIComponent(param_email)+"&aMDP="+encodeURIComponent(param_pwd)
	let url_connection=this.url_ws+ws_function+"?"+ws_param;
	let ws_retour={
			ok: 0,
			Identifiant: "",
			Nom: "",
			Prenom: "",
			AncienneteGroupe: "",
			Adresse: "",
			Ville: "",
			CodePostal: "",
			liberreur: ""
	};

     // don't have the data yet
 	let myHeaders = new Headers();
    myHeaders.append('Accept', 'application/json');
    myHeaders.append('Content-Type', 'application/json');
	
	this.response=null;
	this.data=null;

		let options = new RequestOptions({headers: myHeaders}); 
		this.http.get(url_connection,options) 
		.subscribe(
			data  => {this.data=JSON.stringify(data); 
					let obj=JSON.parse(this.data); 
					let obj2=obj[Object.keys(obj)[0]];
					let obj3=null;
					eval("obj3 = ("+obj2+")");

					ws_retour.Identifiant=obj3.result.Identifiant.trim();
					ws_retour.Nom=obj3.result.Nom.trim();
					ws_retour.Prenom=obj3.result.Prenom.trim();
					ws_retour.AncienneteGroupe=obj3.result.AncienneteGroupe.trim();
					ws_retour.Adresse=obj3.result.Adresse.trim();
					ws_retour.Ville=obj3.result.Ville.trim();
					ws_retour.CodePostal=obj3.result.CodePostal.trim();
					ws_retour.liberreur=obj3.result.liberreur.trim();
					
					if(ws_retour.liberreur==""){ws_retour.ok=1;}
					else {ws_retour.ok=0;}

					alert(ws_retour.Identifiant); //here, I get a value
					},
			err=>alert(err)
			);
alert(ws_retour.Identifiant); //here, the value is empty, why??

If I put a alert(ws_retour.Identifiant) when the call of my web service is a success, I can retrieve the different values (the returned value is a JSON object), but after it’s call, the different values I saved are erased, how I can solve this new problem? the ws_retour variable is used as a returned value to my local function.
Thanks

I’ve got bad news for you: you can’t. This is a fundamental part of how asynchronous programming works. You need to return a transformed Observable from this function and let somebody else subscribe to it.

There are a bunch of other problems here: how the URL is constructed, needless headers, use of eval, pointless JSON interaction, plaintext passwords being stored in server logs. I’m sorry, but I would burn this entire thing to the ground.

Login should be done via POST and return an authentication token. That token should be supplied to this GET request so passwords aren’t in the URL ever. The entire rest can be reduced to something like this:

return this.http.get(url)
  .map(rsp => rsp.json());

If needed, you can add further map transformations on the end of that, but you should start with this and not do any of that eval stuff. The subscription should be separated out to whoever is calling this function. Don’t initiate and subscribe to Observables in the same place.