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

By : dnc253

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.

By : dnc253


I found many times problematic behavior of this whole. I used it from express (without typings) and the bodyParser (with the dt~body-parser typings).

I didn't try to upload a file, instead simply to interpret a JSON given in a post string.

The request.body was simply an empty json ({}).

After a lot of investigation finally this worked for me:

import { json } from 'body-parser';
By : peterh

You can also solve this problem without changing code in server, changing header in $ call and use $_POST the regular way. Explained here:

   .controller('pieChartController', ['$scope', '$http', '$httpParamSerializerJQLike', function($scope, $http, $httpParamSerializerJQLike) {
        var data = {
                TimeStamp : "2016-04-25 12:50:00"
            method: 'POST',
            url: 'serverutilizationreport',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: $httpParamSerializerJQLike(data),
        }).success(function () {});

This video can help you solving your question :)
By: admin