我需要从我的 angularjs 应用程序发送一个 json 数组到一个安静的 api。我正在使用 ngresources 来做到这一点。
从现在起,我已经能够毫无问题地发布和放置单个对象,但现在我需要发送一组对象而我不能。
我试图从外部休息应用程序进行调用,它工作正常,但从我的 Angular 应用程序中这是不可能的。我试图用 JSON.stringify 解析对象,但仍然无法正常工作。我在 $resources 上设置了标题 'Content-Type': 'application/json'。
这就是我如何做 negresource:
.factory('AddSignosClinicos', function ($resource) {
return $resource(dondeapuntar + "/Rest/Pacientedatossignosclinicos.svc/pACIENTEDATOSSIGNOSCLINICOSList/Add", {}, {
create: { method: "POST", headers: { 'Content-Type': 'application/json', params: {} } }
});
})
这就是我调用函数的方式:
var objeto = JSON.stringify(SignosClinicosGuardar);
var signosClinicosService = new AddSignosClinicos(objeto);
signosClinicosService.$create().then(function () {});
我做了一个 objeto 的 console.log,是一个合适的 json 数组。
任何的想法?
非常感谢
编辑
我已经为 post 请求尝试了 $http 组件,它奏效了!我不明白为什么不使用 ngResources,这是我的 $http 代码:
$http({
url: 'http://localhost:1046/Rest/Pacientedatossignosclinicos.svc/pACIENTEDATOSSIGNOSCLINICOSList/Add',
method: "POST",
data: SignosClinicosGuardar,
headers: {
'Content-Type': 'application/json; charset=UTF-8'
}
});
最佳答案
要发布一组对象,您需要添加选项 isArray: true
到您的 $resource:
.factory('AddSignosClinicos', function ($resource) {
return $resource(
"url-string-here",
{},
{
create: {
method: "POST",
isArray: true
}
}
);
})
呼唤新
create
函数看起来像这样://some list of your object instances
var array_of_objects = ...
var saved_objects = AddSignosClinicos.create(
array_of_objects
);
saved_objects.$promise.then(function() {
...
});
请注意,
create
对比 $create
,没有 $
.See the Angular documentation on $resource
关于angularjs - 使用 ANGULARJS $resource 发布一个 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35703804/