【发布时间】:2018-06-05 21:42:41
【问题描述】:
我有一个工作版本,但经过一些更改(我不知道是什么)它不再工作了。现在,我将所有内容都带回到了一个非常简单的应用程序中,以确保没有其他东西妨碍它。是的,我知道这是不好的实践代码,但这没关系,我只想知道这有什么问题。
现在是我的代码,它不是一切,但我认为这是您了解我的代码的作用所需的一切。
角度:
表格:
<form>
<table cellspacing="0">
<tr>
<td>
<input placeholder="Username" class="form-control" [(ngModel)]="username" name="username" required>
</td>
</tr>
<tr>
<td>
<input placeholder="Password" class="form-control" [(ngModel)]="password" type="password" name="password" required>
</td>
</tr></table>
</form>
<button mat-raised-button (click)="loginUser()" class="btn btn-primary" color="primary">Login</button>
组件:
loginUser() : void {
let result = this.LoginService.loginUser(this.username, this.password);
if (result === false) {
this.wrongLoginNotification = 'Je hebt de onjuiste inloggegevens gebruikt!';
}
}
服务:
private loginUrl = 'http://www.url.nl/path/api/?login=true';
loginUser(username: string, password: string): boolean {
let auth = {username: username, password: password};
let authJson = JSON.stringify(auth);
this.http.post(this.loginUrl, authJson)
.subscribe(
data => {
this.router.navigate(["home"]);
return true;
},
err => {
return false;
}
);
return false;
}
此帖子到服务器:
{password: "TEST", username: "TEST1"}
PHP脚本很简单,仅此而已:
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$servername = ""; // Changed this of course ;-)
$username = ""; // Changed this of course ;-)
$password = ""; // Changed this of course ;-)
$dbName = ""; // Changed this of course ;-)
$conn = null;
try
{
$conn = new PDO("mysql:host=$servername;dbname=$dbName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
header("HTTP/1.1 404");
}
if (isset($_GET['login'])) {
echo 'Test';
}
现在 PHP api(在同一个域/url 上)返回:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /myurl/api/
on this server.<br />
</p>
<p>Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
我尝试了什么?
- 我确实检查了文件夹的权限:755,文件:644。
- GET 仍然有效,POST 无效。
-
阅读 StackOverflow 上的几篇文章。使用 .htaccess 文件尝试了几件事,例如添加:
标头添加 Access-Control-Allow-Origin: * 标头添加 Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
我也在联系我的托管服务。有人有什么建议吗? 我确实看到很多关于这个主题的问题,每个人都有其他解决方案。过滤这个并找到正确的点的最佳方法是什么?我现在只是在尝试一些事情,而没有遵循列表或可能让我找到解决方案的事情。
非常欢迎任何帮助!
【问题讨论】:
标签: php angular post pdo http-status-code-403