本文介绍了AngularJS:将发送数据与URL但不作为JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的 UserService
angular.module('userServices', ['ngResource']).factory('User', function($resource) {
return $resource('/users/:userId',
// todo: default user for now, change it
{userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810'},
{update: {method: 'PUT', params:{profile: '@profile'}, isArray: false}}
);
});
在我的控制器,我
$scope.save = function() {
$scope.user.$update({profile: $scope.profile});
}
但是,当我看到网络选项卡在Chrome中,我看到了
But when I see the Network Tab in Chrome, I see
Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810?profile=%5Bobject+Object%5D
Request Method:PUT
Status Code:200 OK
我怎样才能发送此为数据
负载?让网址
是
http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
和数据都按
{
day_in_month: 5
}
我的端点预计的数据是请求部分,以便它可以解析为 request.json
感谢您
推荐答案
@lucuma回答解决我的问题。
@lucuma answer solved my problem.
我分享从我的code基地code这使得变化每@ lucuma的建议后工作(非常感谢@lucuma!)
I am sharing the code from my code base which worked after making changes as per @lucuma's suggestion (Thanks a lot @lucuma!)
的 UserService
看起来
angular.module('userServices', ['ngResource']).factory('User', function($resource) {
return $resource('/users/:userId',
// todo: default user for now, change it
{userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810'},
{update: {method: 'PUT', data:{}, isArray: false}} // add data instead of params
);
});
和 ProfileController可
看起来
function ProfileController($scope, User) {
$scope.profile = {};
$scope.user = User.get();
$scope.save = function () {
// I was using $scope.user.$update before which was wrong, use User.update()
User.update($scope.profile,
function (data) {
$scope.user = data; // since backend send the updated user back
});
}
做了这些改变之后,我在Chrome我的网络选项卡中如预期
After making these changes, I my network tab in Chrome was as expected
Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
Request Method:PUT
Status Code:200 OK
Request Payload:
{"day_in_month":25}
这篇关于AngularJS:将发送数据与URL但不作为JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!