Javascript userServic不会将数据传递到后端:无法读取未定义-AngularJS的属性“protocol”

Javascript userServic不会将数据传递到后端:无法读取未定义-AngularJS的属性“protocol”,javascript,html,angularjs,model-view-controller,scope,Javascript,Html,Angularjs,Model View Controller,Scope,我的用户控制面板上的“更改密码”功能不起作用。它从输入中提取值,并将它们从控制器传递到服务,服务接收它们,但当他用它们发送到后端时,调试控制台返回:无法读取未定义的属性“protocol” 重要: 在编写解决方案之前,请参阅我的预览问题,以了解为什么我必须使用对象的字段来定义输入范围。请考虑将整个对象发送到后端不是一个选项,谢谢。 Form.html: <accordion-group heading="Change password" is-open="changePasswordSta

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

重要: 在编写解决方案之前,请参阅我的预览问题,以了解为什么我必须使用对象的字段来定义输入范围。请考虑将整个对象发送到后端不是一个选项,谢谢。 Form.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>
userService.js:

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

    };
});
调试:

控制台中的错误:

根据,post方法参数是

网址 数据对象 选项对象 那么,试试这个:

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

    };
});

我设法解决了这个问题。有三种解决办法。在后端,我有一个Java控制器,它被配置为使用POST方法接受@RequestBody。首先,它被配置为两个字符串,但碰巧Angular的$HTTPPOST将接收三个PARM-1。url,2.data,3。将对象配置为@RémiB。他指出。所以合乎逻辑的解决方案是发送对象,并将其作为通用对象(如对象数据)接收,但该对象没有getter和setter,因此无法提取数据。绝对不能接受的解决方案是使用为此类数据设计的getter和setter创建实用程序类。不太可接受的解决方案是向现有类用户添加新的字段newPassword,以便可以处理通过该类接收的数据。第三个可疑的解决方案是将数据作为通用对象接收,并对该对象执行toString操作,然后编写代码从字符串中提取子字符串。这听起来很不对,我不想那样做。最后一个解决方案是最不可接受的:更改方法以从Angular服务获取和发送url中的数据,并通过Java后端的@PathVariables将其作为字符串进行响应。长话短说:

changePassword : function(newPassword, currentPassword) {
    $http.get('/user/changePassword/'+newPassword+'/'+currentPassword);
}
在后端:

@RequestMapping(value = "/changePassword/{newPassword}/{currentPassword}", method = RequestMethod.GET)
    public HashMap<String, String> changePassword(@PathVariable("newPassword") String newPassword, @PathVariable("currentPassword") String currentPassword) {  
....
}

您正在将currentPassword作为选项对象传递。看到了吗,发送了它,但是作为后端的@RequestBody,我应该期望什么呢?现在看起来是这样的:changePassword@RequestBody最后一个字符串newPassword,@RequestBody final String currentPassword我知道我将接收对象,如果我放置@RequestBody final object data,如果我没有geters和seters,如何从该对象获取fild?我认为您最好创建一个新问题来帮助您解决这一点,因为它是关于后端的,所以它不是关于angularJS的,但是。。。。CJAVA你可以验证我的答案是否正确,并用你的新困难提出一个新问题。您可以在此处添加一个链接,以引用您工作的连续性。
@RequestMapping(value = "/changePassword/{newPassword}/{currentPassword}", method = RequestMethod.GET)
    public HashMap<String, String> changePassword(@PathVariable("newPassword") String newPassword, @PathVariable("currentPassword") String currentPassword) {  
....
}