AngularJS:PUT使用URL发送数据,但不作为JSON数据发送数据

day*_*mer 1 javascript flask angularjs angularjs-service angularjs-scope

这是我的 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}}
  );
});
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我做到了

$scope.save = function() {
    $scope.user.$update({profile: $scope.profile});
}
Run Code Online (Sandbox Code Playgroud)

但是当我在Chrome中看到网络标签时,我明白了

Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810?profile=%5Bobject+Object%5D
Request Method:PUT
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)

如何将其作为data有效载荷发送?这URL就是

http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
Run Code Online (Sandbox Code Playgroud)

数据如下

{
  day_in_month: 5
}
Run Code Online (Sandbox Code Playgroud)

我的端点期望数据是请求的一部分,因此它可以将其解析为 request.json

谢谢

day*_*mer 10

@lucuma的答案解决了我的问题.

我正在根据@ lucuma的建议在我的代码库中共享代码,这些代码库是根据@lucuma的建议进行更改后工作的(非常感谢@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
  );
});
Run Code Online (Sandbox Code Playgroud)

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
            });
    }
Run Code Online (Sandbox Code Playgroud)

进行这些更改后,我在Chrome中的网络标签符合预期

Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
Request Method:PUT
Status Code:200 OK
Request Payload:
{"day_in_month":25}
Run Code Online (Sandbox Code Playgroud)


luc*_*uma 8

我建议您对更新声明进行以下更改:

{update: {method: 'PUT', data:{profile:'@profile'}, isArray: false}}

查看此plunker上的网络选项卡.-v.1.1.5

这是稳定版本1.0.7上的相同示例.