gpt4 book ai didi

javascript - userServic不会将数据传递到后端: Cannot read property 'protocol' of undefined - AngularJS

转载 作者:行者123 更新时间:2023-11-28 08:00:03 25 4
gpt4 key购买 nike

我的用户控制面板上的更改密码功能不起作用。它从输入中获取值并将它们从 Controller 传递到服务,服务接收它们,但是当他尝试使用它们到达后端时,调试控制台返回:无法读取未定义的属性“协议(protocol)”。

重要:请引用我的预览问题here在编写解决方案之前,请先了解为什么我必须使用对象的字段来确定输入范围。请考虑将整个对象发送到后端不是一个选项,谢谢。

表单 (.html):

<accordion-group heading="Change password" is-open="changePasswordStatus.open" style="cursor: pointer">
<div>
<div>
<form class="form-horizontal" role="form">
<form-row model="password.newPassword" name="New: " size="col-sm-8"></form-row>
<form-row model="password.repeatedNewPassword" name="Repeat: " size="col-sm-8"></form-row>
<form-row model="password.currentPassword" name="Current: " size="col-sm-8"></form-row>
<br>
</form>
</div>
<div>
<button class="btn btn-default btn-sm" data-ng-click="changePassword()">Save</button>
<button class="btn btn-warning btn-sm" ng-click="changePasswordStatus.open = !changePasswordStatus.open">Cancel</button>
</div>
</div>
</accordion-group>

userController.js:

collectionsApp.controller('userController', function($scope, userService, $timeout, $state) {
$scope.password = {
newPassword : "",
repeatedNewPassword : "",
currentPassword : ""
}

$scope.init = function() {
userService.getCurrentUser(getCurrentUser);
}

$scope.changePassword = function() {
if ($scope.password.newPassword === $scope.password.repeatedNewPassword) {
userService.changePassword($scope.password.newPassword, $scope.password.currentPassword);
}
}

...

userService.js:

collectionsApp.service('userService', function($http) {
return {
changePassword : function(newPassword, currentPassword) {
$http.post('/user/changePassword', newPassword, currentPassword);
}

};
});

调试:

enter image description here

控制台错误:

enter image description here

最佳答案

根据$http reference ,post 方法参数是

  1. 网址
  2. 数据对象
  3. 选项对象

所以,试试这个:

collectionsApp.service('userService', function($http) {
return {
changePassword : function(newPassword, currentPassword) {
var data = {
newPassword: newPassword,
currentPassword: currentPassword
};
$http.post('/user/changePassword', data);
}

};
});

关于javascript - userServic不会将数据传递到后端: Cannot read property 'protocol' of undefined - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25548611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com