【发布时间】:2016-06-12 17:50:08
【问题描述】:
我需要从我的 angularjs 应用程序发送一个 json 数组到一个 restful api。我正在使用 ngresources 来执行此操作。 从现在开始,我已经可以毫无问题地发布和放置单个对象,但是现在我需要发送一个对象数组,但我不能。
我尝试从外部休息应用程序进行调用,它工作正常,但从我的角度应用程序中是不可能的。我尝试使用 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'
}
});
【问题讨论】:
-
请提供一段代码并深入挖掘 json stringify 错误。
-
哦,对不起。我已经添加了上面的代码。谢谢
-
尝试将选项
isArray: true添加到您返回的$resource。 See ng docs on $resource
标签: angularjs post ngresource