我有这个 API 函数:
public function login($conn, $user){
$login = "SELECT username FROM login WHERE username=:user";
$execLogin = $this->conn->prepare($login);
$execLogin->bindValue(":user", $user);
$execLogin->execute();
$res = $execLogin->rowCount();
try{
return $res;
}
catch(PDOException $e)
{
return $e->getMessage();
}
}
这是单击登录按钮时的 php 脚本:
<button id="btn-login" (click)="login()" [disabled]="!loginGroup.valid" class="btn btn-success">Login </button>
登录功能的脚本将运行:
<?php
require_once('../api.php');
//Getting username and password from Angular
$user = $_POST['user'];
$newApi = new api();
$conn = $newApi->connection();
$res = $newApi->login($conn, $user);
?>
这是打字稿中使用的方法。在 API 服务中:
login(username, password)
{
let headerOptions = new HttpHeaders();
headerOptions.append('Access-Control-Allow-Origin', '*');
headerOptions.append('Access-Control-Request-Headers', '*');
headerOptions.append('Content-Type', 'application/json');
headerOptions.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
this.credentials = {user: username, pass: password};
const httpParams = new HttpParams();
httpParams.set('user', username);
httpParams.set('pass', password);
//console.log("hi "+ this.credentials);
return this.http.post('http://dev.local/scripts/login.php', {username, password}, {
observe: 'response',
responseType: 'text',
headers: headerOptions
}).pipe(map(
res=>{
console.log(res)
},
err=>
console.log(err)
))
}
并单击按钮:
login(){
let user = this.loginGroup.get('username').value;
let pass = this.loginGroup.get('password').value;
this.auth.login(user, pass).subscribe(
(data)=>{
console.log(data)
},
(error)=>{
console.log(error)
}
)
}
问题是我总是在控制台的网络选项卡上看到以下错误:
call to undefined variable user at line 7
似乎用户不是通过打字稿方法发送到 PHP 脚本的。
编辑