Retrieve a json web service datuum


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');

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

					else {ws_retour.ok=0;}

					alert(ws_retour.Identifiant); //here, I get a value
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.

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.