-2

Am using Angular2 and Php as my server side scripting language. I want to create a session in Php and returned to Angular2 and this session is keep until it expires .

in angular2, I used a function when the login button is pressed

 save()
    {
         
              var ret = this.http.get('http://localhost/php/login.php?value='+this.Username+'&value2='+this.Password)
                .subscribe();
                
                alert(ret); // alerting [Object Object]
                
    }

and in my server side

<?php
$user = $_GET['value'];
$pass=$_GET['value2'];
$result = $user.$pass;
// here i want to create the session and return
// now i simply returns a number
return 1;
?>

what is the best solution for this ..?

vinu prasad
  • 169
  • 1
  • 3
  • 13
  • 3
    Don't pass a password via URL address. Don't store pass in plaintext. Need to create a session? Where is problem? Want to return a number? What number? Where is the problem? – pavel Nov 03 '17 at 10:57
  • alerting an object shows [Object object]. Do a console log instead to see values of the object: `console.log(ret);` – kscherrer Nov 03 '17 at 10:58
  • .. @panther , my php page returns a number . This returned value should be accessed at angular side ..but it won't – vinu prasad Nov 03 '17 at 11:02
  • .@panther , I changed URL to POST and use an encode before passing it , as you said . But how can I store the returning session in my Angular2 ? – vinu prasad Nov 04 '17 at 03:57

1 Answers1

0
var ret = this.http.get(url)
            .subscribe();

Angular works with Observers (rxjs). The http.get() function will return you an subscription object. If you want to log the result you need to log the value in your subscribe method.

this.http.get(url).subscribe((res:Response)=> console.log(res.json()));

Don't use credentials in the url params. Instead of Get use the Post method to send your Data to your server.

this.http.post(url, data).subscribe();
Muhammed Misir
  • 400
  • 9
  • 22
  • thanks for the detailed info@mimu1011 . But actually I want to keep the returned value in a localstorage . Then what should I do . Am using a LocalStorageService to store data. – vinu prasad Nov 03 '17 at 11:17
  • You can call in the subscribe function the localstorageservice to store the data. .subscribe((res:Response) => saveInLocal(res.json())) – Muhammed Misir Nov 03 '17 at 12:10
  • I save the return result from php as you said like : .subscribe((res:Response) => this.localStorage.set('aa',res.json())) , But it shows null value in storage @mimu1011 – vinu prasad Nov 04 '17 at 03:54