如何使用 ngResource 在 Angular 中发送带有 body 的 put-request
How to send a put-request with body in Angular with ngResource
我可以用 ngResource 发送 put-request 和 header。我的 FactoryService 看起来像这样:
angular
.module("playersServicesModule", ["ngResource", "config"])
.factory("playersService", ["$resource", "API_ROOT",
function ($resource, API_ROOT) {
"use strict";
var url = API_ROOT + "/api/footballplayers";
return {
updateFootballPlayer: function (id, column, newValue) {
return $resource(url + '/:Id', { Id: id },
{
"update": {
method: 'PUT', headers: {
"Column": column,
"NewValue": newValue
}
}
});
}
};
如何向 put-request 的 body 添加数据?
更新
对您的工厂的建议更新如下:
angular
.module("playersServicesModule", ["ngResource", "config"])
.factory("playersService", ["$resource", "API_ROOT",
function ($resource, API_ROOT) {
"use strict";
var url = API_ROOT + "/api/footballplayers";
var myResource = $resource(url + '/:Id',
{ Id: '@id },
{
"update": {
method: 'PUT'
}
});
return {
updateFootballPlayer: function (id, column, newValue) {
return myResource.update(
{Id: id},
{
column: column,
newValue: newValue
},
function (successResponse) {
// Do something on success
},
function (failResponse) {
// Do something on fail
}
);
}
};
});
原创
您在执行实际请求时向正文添加数据,例如
$resource(url + '/:Id', { Id: id },
{
"update": {
method: 'PUT',
headers: {
"Column": column,
"NewValue": newValue
}
}
}
).update(
{},
<BODY_OBJECT>,
function (successResponse) {},
function (failResponse) {}
);
您要作为正文数据传递的对象将替换 <BODY_OBJECT>
。
我可以用 ngResource 发送 put-request 和 header。我的 FactoryService 看起来像这样:
angular
.module("playersServicesModule", ["ngResource", "config"])
.factory("playersService", ["$resource", "API_ROOT",
function ($resource, API_ROOT) {
"use strict";
var url = API_ROOT + "/api/footballplayers";
return {
updateFootballPlayer: function (id, column, newValue) {
return $resource(url + '/:Id', { Id: id },
{
"update": {
method: 'PUT', headers: {
"Column": column,
"NewValue": newValue
}
}
});
}
};
如何向 put-request 的 body 添加数据?
更新
对您的工厂的建议更新如下:
angular
.module("playersServicesModule", ["ngResource", "config"])
.factory("playersService", ["$resource", "API_ROOT",
function ($resource, API_ROOT) {
"use strict";
var url = API_ROOT + "/api/footballplayers";
var myResource = $resource(url + '/:Id',
{ Id: '@id },
{
"update": {
method: 'PUT'
}
});
return {
updateFootballPlayer: function (id, column, newValue) {
return myResource.update(
{Id: id},
{
column: column,
newValue: newValue
},
function (successResponse) {
// Do something on success
},
function (failResponse) {
// Do something on fail
}
);
}
};
});
原创
您在执行实际请求时向正文添加数据,例如
$resource(url + '/:Id', { Id: id },
{
"update": {
method: 'PUT',
headers: {
"Column": column,
"NewValue": newValue
}
}
}
).update(
{},
<BODY_OBJECT>,
function (successResponse) {},
function (failResponse) {}
);
您要作为正文数据传递的对象将替换 <BODY_OBJECT>
。