AngularJS – Any way for $ to send request parameters instead of JSON?

I have some old code that is making an AJAX POST request through jQuery’s post method and looks something like this:

$.post("/foo/bar", requestData,
        //do stuff with response

requestData is just a javascript object with some basic string properties.

I’m in the process of moving our stuff over to use Angular, and I want to replace this call with $ I came up with the following:

$"/foo/bar", requestData).success(
    function(responseData) {
        //do stuff with response

When I did this, I got a 500 error response from the server. Using Firebug, I found that this sent the request body like this:


The successful jQuery $.post sends the body like this:


The endpoint I am hitting is expecting request parameters and not JSON. So, my question is is there anyway to tell $ to send up the javascript object as request parameters instead of JSON? Yes, I know I could construct the string myself from the object, but I want to know if Angular provides anything for this out of the box.


I think the params config parameter won’t work here since it adds the string to the url instead of the body but to add to what Infeligo suggested here is an example of the global override of a default transform (using jQuery param as an example to convert the data to param string).

Set up global transformRequest function:

var app = angular.module('myApp');

app.config(function ($httpProvider) {
    $httpProvider.defaults.transformRequest = function(data){
        if (data === undefined) {
            return data;
        return $.param(data);

That way all calls to $ will automatically transform the body to the same param format used by the jQuery $.post call.

Note you may also want to set the Content-Type header per call or globally like this:

$['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

Sample non-global transformRequest per call:

    var transform = function(data){
        return $.param(data);

    $"/foo/bar", requestData, {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
        transformRequest: transform
    }).success(function(responseData) {
        //do stuff with response

Leave a Reply

Your email address will not be published. Required fields are marked *