AngularJS$http post请求被取消
我试图向PHP脚本发送一个post表单,每次post请求都显示为已取消 我的控制器如下所示:AngularJS$http post请求被取消,angularjs,forms,post,Angularjs,Forms,Post,我试图向PHP脚本发送一个post表单,每次post请求都显示为已取消 我的控制器如下所示: var authenticationControllers = angular.module('authenticationControllers', []); authenticationControllers.controller('Authentication', ['$scope', '$http', function($scope, $http) { $scope.WPlogin
var authenticationControllers = angular.module('authenticationControllers', []);
authenticationControllers.controller('Authentication', ['$scope', '$http', function($scope, $http) {
$scope.WPlogin = function () {
var mydata ={
'action': 'ajaxlogin',
'username': $scope.user.username,
'password': $scope.user.password,
'security-login': pluginParams.nonce,
'_wp_http_referer': '/s/play/'
};
$http({
method: 'POST',
url: $scope.ajaxURL, //This is defined earlier, I'm sure this is valid
data: jQuery.param(mydata),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
console.log(data.message);
}).error(function(e){alert('error')});
};
}]);
<div class="white-popup mfp-hide" id="login-modal" ng-controller="Authentication">
<form name="loginform" id="loginform" novalidate>
<h1>Login</h1>
<p class="status"></p>
<label for="username">Username</label>
<input id="username" type="text" name="username" ng-model="user.username" ng-required="true">
<label for="password">Password</label>
<input id="password" type="password" name="password" ng-model="user.password" ng-required="true">
<a class="lost" href="<?php echo wp_lostpassword_url(); ?>">Lost your password?</a>
<button class="submit_button" ng-disabled="loginform.$invalid" ng-click="WPlogin()" type="submit" name="submit"> Login</button>
</form>
</div>
表格如下所示:
var authenticationControllers = angular.module('authenticationControllers', []);
authenticationControllers.controller('Authentication', ['$scope', '$http', function($scope, $http) {
$scope.WPlogin = function () {
var mydata ={
'action': 'ajaxlogin',
'username': $scope.user.username,
'password': $scope.user.password,
'security-login': pluginParams.nonce,
'_wp_http_referer': '/s/play/'
};
$http({
method: 'POST',
url: $scope.ajaxURL, //This is defined earlier, I'm sure this is valid
data: jQuery.param(mydata),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
console.log(data.message);
}).error(function(e){alert('error')});
};
}]);
<div class="white-popup mfp-hide" id="login-modal" ng-controller="Authentication">
<form name="loginform" id="loginform" novalidate>
<h1>Login</h1>
<p class="status"></p>
<label for="username">Username</label>
<input id="username" type="text" name="username" ng-model="user.username" ng-required="true">
<label for="password">Password</label>
<input id="password" type="password" name="password" ng-model="user.password" ng-required="true">
<a class="lost" href="<?php echo wp_lostpassword_url(); ?>">Lost your password?</a>
<button class="submit_button" ng-disabled="loginform.$invalid" ng-click="WPlogin()" type="submit" name="submit"> Login</button>
</form>
</div>
登录
用户名
密码
登录
每当我尝试提交表单时,都会弹出错误警报,但不会出现控制台错误。然后,页面重新加载,我可以在URL中看到表单参数(如get请求),如果我提交表单(不删除get参数),则请求成功
谁能告诉我我做错了什么
更新
我添加了
$event.preventDefault()代码>到表单(从表单传递$event),现在一切都按预期工作,但我不明白,为什么我需要它?我以为AngularJS会自动阻止表单提交。1。你在plunker上的代码:在那里我删除了pluginParams.nonce
和jQuery.param(mydata)
。它确实发布了。。。2.为什么使用jQuery方法?3.如果你有http拦截器,请检查。我的浏览器可能有问题,因为你的插件也会触发错误警报。我使用jQuery向脚本发送常规查询字符串,而不是JSON: