anglar ajax执行2次的原因,AngularJS与Ajax表单提交需要单击两次

我需要从HTML页面执行以下活动:

用户输入电子邮件和密码进行注册

用户单击时将表格发送到控制器 Submit

Control使用AJAX创建到RESTful Server的JSON请求,服务器进行相应的响应。

根据服务器的响应,用户应获得警报窗口,并register.html在“提交”下方的当前页面()上显示一条消息。如果是“成功-已注册”或“失败-无法注册”。

但是,目前它的工作方式如下:1),2),3)正常工作。

用户在第一次单击Submit相应的消息时会得到警报窗口。

用户仅在Submit 再次 单击时获得打印的消息,但再次显示警告窗口。

如果我alert('Something')从JS中删除,则必须单击两次以将消息打印在register.html

另外,我想提醒您注意,单击两次也会使服务器调用两次。它的行为就好像在服务器调用后已暂停,然后打印我应该单击“提交”的消息一样。

register.html

Submit

{{msgalert}}

我的JS控制器看起来像这样 stackoverflow_q3.js

// create angular controller

var register = angular.module('Main-app');

register.controller('registerController', function ($scope, $http, $location) {

// function to submit the form after all validation has occurred

$scope.submitForm = function (isValid, user) {

console.log('Stackoverflow JS func caled');

var regData = {

"email": user.email,

"password": user.password1

};

var jsonData = JSON.stringify(regData);

var request = $.ajax({

url: 'myurl',

type: 'POST',

data: jsonData,

headers: {

'Accept': 'application/json',

'Content-Type': 'application/json'

},

dataType: 'json',

complete: function (response) {

console.log(response.responseText);

if (response.responseText == 'success') {

console.log('Registration Success');

alert('Success');

$scope.msgalert = 'Registration Success, Proceed to Login and Continue';

} else if (response.responseText == 'fail') {

alert('registration failed');

$scope.msgalert = 'Registration Failed, Please try again';

}

}

});

};

});

另外index.html我提到了控制器:

知道如何通过一次“提交”单击即可完成所有这些活动吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值