POST JSON array with ANGULARJS $ resource

I need to send a json array to a soothing api from my angularjs application. I use ngresources for this. Since then, I have been removed to publish and place one object without problems, but now I need to send an array of objects, and I can not.

I tried to make a call from an external leisure application and it works fine, but this is not possible from my angular application. I have a trie to parse an object with JSON.stringify, but still not working. I set the header to 'Content-Type': 'application / json' as well as in $ resources.

This is how I do negresource:

.factory('AddSignosClinicos', function ($resource) { return $resource(dondeapuntar + "/Rest/Pacientedatossignosclinicos.svc/pACIENTEDATOSSIGNOSCLINICOSList/Add", {}, { create: { method: "POST", headers: { 'Content-Type': 'application/json', params: {} } } }); }) 

And this is how I call the function:

 var objeto = JSON.stringify(SignosClinicosGuardar); var signosClinicosService = new AddSignosClinicos(objeto); signosClinicosService.$create().then(function () {}); 

I created the console.log of the objeto object and is the correct json array.

Any idea?

Thank you very much

EDIT

I tried the $ http component for the mail request and it will work! I do not understand why it does not work with ngResources, this is my code for $ http:

  $http({ url: 'http://localhost:1046/Rest/Pacientedatossignosclinicos.svc/pACIENTEDATOSSIGNOSCLINICOSList/Add', method: "POST", data: SignosClinicosGuardar, headers: { 'Content-Type': 'application/json; charset=UTF-8' } }); 
+5
source share
1 answer

To publish an array of objects, you need to add the isArray: true option to your $ resource:

 .factory('AddSignosClinicos', function ($resource) { return $resource( "url-string-here", {}, { create: { method: "POST", isArray: true } } ); }) 

A call to the new create function will look something like this:

 //some list of your object instances var array_of_objects = ... var saved_objects = AddSignosClinicos.create( array_of_objects ); saved_objects.$promise.then(function() { ... }); 

Note that create vs $create not $ .

See the Angular documentation on the $ resource

+5
source

Source: https://habr.com/ru/post/1244129/


All Articles