how to send x-www-form-urlencoded data using ngResource module with angular?
Asked Answered
W

3

3

everything lives in the title.

when producing a resource in angular :

myModule.factory('MyResource', ['$resource', function ($resource) {

    return $resource('api/MyResource/:id');

}]);

and using in a controller :

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

the Service sends the data in a json artifact ahead to the server.

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

Where ought I modify my code to resolve that ?

Wellhead answered 19/3, 2015 at 16:16 Comment(0)
W
-3

I finally found myself:

When defining a resource and the associated instruction, the "headers" parameter comes in hand.

myModule.factory('MyResource', ['$resource', function ($resource) {

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

}]);
Wellhead answered 20/3, 2015 at 9:1 Comment(2)
still data is sent as jsonPyorrhea
the parameters should be serializedKeir
K
3

Should pass the headers parameters

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 them with $httpParamSerializer

myModule.controller('appController', function ($httpParamSerializer) {
    MyResource.save($httpParamSerializer({att: att, att2: att2}));
}
Keir answered 17/12, 2015 at 9:10 Comment(1)
$httpParamSerializer save my dayPolecat
L
0

Complete answer (since angular 1.4). You need to include de 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) { 

            });
Licorice answered 20/11, 2015 at 21:40 Comment(0)
W
-3

I finally found myself:

When defining a resource and the associated instruction, the "headers" parameter comes in hand.

myModule.factory('MyResource', ['$resource', function ($resource) {

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

}]);
Wellhead answered 20/3, 2015 at 9:1 Comment(2)
still data is sent as jsonPyorrhea
the parameters should be serializedKeir

© 2022 - 2024 — McMap. All rights reserved.