how to send x-www-form-urlencoded data using ngRes

2019-05-15 00:52发布

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 ?

3条回答
smile是对你的礼貌
2楼-- · 2019-05-15 01:20

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}));
}
查看更多
3楼-- · 2019-05-15 01:27

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) { 

            });
查看更多
The star\"
4楼-- · 2019-05-15 01:43

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' } }
    });

}]);
查看更多
登录 后发表回答