How to send x-www-form-urlencoded data using ngResource module using angular?

everything lives in the name.

when creating a resource in angular:

myModule.factory('MyResource', ['$resource', function ($resource) { return $resource('api/MyResource/:id'); }]); 

and use in the controller:

 MyResource.save({att: att, att2: att2}); 

The service sends data to the json artifact forward to the server.

I need to send data in the form x-www-form-urlencoded .

Where should I change my code to solve this problem?

+3
source share
3 answers

Finally I discovered:

When defining a resource and associated instructions, the headers parameter is included.

 myModule.factory('MyResource', ['$resource', function ($resource) { return $resource('api/MyResource/:id', {}, { save: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } }); }]); 
-3
source

Headers must be passed

 myModule.factory('MyResource', ['$resource', function ($resource) { return $resource('api/MyResource/:id', {}, { save: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } }); }]); 

then serialize your data before sending it using $httpParamSerializer

 myModule.controller('appController', function ($httpParamSerializer) { MyResource.save($httpParamSerializer({att: att, att2: att2})); } 
+3
source

Full answer (from angular 1.4). You need to enable the dependency $ httpParamSerializer

 var res = $resource(serverUrl + 'Token', { }, { save: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } }); res.save({ }, $httpParamSerializer({ param1: 'sdsd', param2: 'sdsd' }), function (response) { }, function (error) { }); 
0
source

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


All Articles