The comparable jQuery function
$.post("/example/handler", {foo: 1, bar: 2});
will create a request with the post parameters foo=1&bar=2. Whereas
$http.post("/example/handler", {foo : 1, bar: 2});
seems to send a POST request with the body {"foo":1,"bar":2} rather than the form-uriencoded version. To get what I think is the expected behavior here, I need to do something like
myModule.config(function ($httpProvider) {
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$httpProvider.defaults.transformRequest = function(data){
return _.map(data, function (val, k) { return encodeURI(k) + "=" + encodeURI(val); }).join("&");
}
});
in the module config.
Can anyone explain the rationale behind $http.post argument handling? Is there a situation where I'd want Angulars' default behavior, or some hidden advantage I'm not seeing?