使用Angular2和Php作为我的服务器端脚本语言。我想在Php中创建一个会话并返回Angular2,这个会话一直保持到期满为止。
在angular2中,我在按下登录按钮时使用了一个功能
save()
{
var ret = this.http.get('http://localhost/php/login.php?value='+this.Username+'&value2='+this.Password)
.subscribe();
alert(ret); // alerting [Object Object]
}
在我的服务器端
<?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;
?>
对此最佳解决方案是什么??
答案 0 :(得分:0)
var ret = this.http.get(url)
.subscribe();
Angular与Observers(rxjs)合作。 http.get()函数将返回一个订阅对象。如果要记录结果,则需要在订阅方法中记录该值。
this.http.get(url).subscribe((res:Response)=> console.log(res.json()));
不要在网址参数中使用凭据。而不是使用Post方法将您的数据发送到您的服务器。
this.http.post(url, data).subscribe();