AngularJS: PUT用URL发送数据,而不是JSON数据
AngularJS: PUT send data with URL but not as JSON data
这是我的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浏览器中看到网络选项卡时,我看到
Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810?profile=%5Bobject+Object%5D
Request Method:PUT
Status Code:200 OK
我如何将其作为data
有效载荷发送?使URL
为
http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
和data作为
{
day_in_month: 5
}
我的端点希望数据是请求的一部分,因此它可以将其解析为request.json
谢谢
@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
);
});
和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浏览器中的网络选项卡如预期的
Request URL:http://localhost:5000/users/bd675d42-aa9b-11e2-9d27-b88d1205c810
Request Method:PUT
Status Code:200 OK
Request Payload:
{"day_in_month":25}
我建议你对你的更新声明做如下修改:
{update: {method: 'PUT', data:{profile:'@profile'}, isArray: false}}
查看这个柱塞上的网络标签。-v.1.1.5
下面是稳定版1.0.7的相同示例
相关文章:
- 无法使用变量访问数据 JSON
- 使用 ng-repeat解析不均匀数据/ json对象
- 在没有jsonp的情况下从另一个域获取数据(json格式)
- 如何获取带参数的数据json
- 如何在android中显示实时雅虎金融股票数据..json格式如下所示
- 使用 JavaScript 将数据 json 显示到网页中
- 高位图表来源于API数据(JSON)
- 使用动态数据json初始化同位素
- Django:将数据JSON从视图传递给javascript
- 存储配置数据 (json)
- 未捕获的SyntaxError:意外的标识符图像数据json
- 接收数据json/jquery
- Jquery数据表数据json
- 不能在cakephp中使用数组数据json
- 在不锁定浏览器的情况下,将大数据JSON从REST请求反序列化为对象
- 如何读取数据JSON格式数组的字符串数据
- 我有一个流星应用程序,需要从/public/_assets/results/mmresults读取数据.Json文件,在
- 数据json不显示在html
- 使用2个下拉列表创建2个并排的表行来比较数据- JSON
- 需要将复杂的json对象转换为合适的angularjs UI树数据json结构