我有一个角度 js 应用程序,它尝试登录用户并将 $_REQUEST['email'] 和 $_REQUEST['password'] 发送到我的 PHP 脚本,但脚本报告未定义索引:电子邮件以及未定义索引:密码。
我试过直接输入“电子邮件”和“密码”,而不从角度解析,但它不起作用;我还尝试将电子邮件和密码从 jQuery 中的普通旧 ajax 查询发送到脚本,它可以工作,我得到了一个令牌;它只是从角度或反应中不起作用。
html:
<form>
<p><span class="sr-only"><label for="email">Email</label></span><input class="form-control" type="text" class="txt" placeholder="Email" ng-model="user.email" name="email" id="email"></p>
<p><span class="sr-only"><label for="password">Password</label></span><input class="form-control" type="password" class="txt" placeholder="Password" ng-model="user.password" name="password" id="password"></p>
<p><input type="submit" id="_submit" value="Login" class="btn-info" ng-click="login(user)"/></p>
</form>
角度登录脚本:
$scope.login = function(user) {
$scope.showError = false;
$scope.showValidationError = false;
$scope.showRateError = false;
$scope.showNoUserError = false;
if(angular.isUndefined(user)) {
$scope.showValidationError = true;
$state.go('login');
}
$http.post("https://myapi.com/login", { email: user.email, password: user.password })
.then(function (result) {
$state.go('dash');
}, function(error) {
if(error.status == 401) {
$scope.showError = true;
}
if(error.status == 403) {
$scope.showRateError = true;
}
if(error.status == 422) {
$scope.showValidationError = true;
}
if(error.status == 500) {
$scope.showInternalError = true;
}
console.log("login: " + error.status + " " + error.statusText);
});
}
当脚本开始破折号时,它会尝试刷新并且我收到此错误:
Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Bearer
Notice: Undefined index: email in /var/www/html/script.php on line 713
Notice: Undefined index: password in /var/www/html/script.php on line 714
我试图在我的 php 脚本和 Apache 上设置 CORS
在 script.php 的顶部:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Authorization');
在虚拟主机配置的一部分:
<Directory /var/www/html>
Header set Access-Control-Allow-Origin "*"
</Directory>
如果我从 jQuery 发送请求,它工作正常:
$(document).ready(function() {
$("#login").click(function() {
var email = $("#email").val();
var password = $("#password").val();
var formData = {email: email, password: password};
$.ajax({
url: "https://myapi.com/login",
type: "POST",
data: formData,
success: function(data, textStatus, jqXHR)
{
console.log(textStatus);
console.log(data);
if(data != "") {
var data = JSON.parse(data);
console.log(data.token);
$("#token").html(data.token);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(textStatus);
console.log(errorThrown);
}
});
});
});
不知道从这里做什么其他故障排除。任何帮助表示赞赏。