1

I know it has been solved here many times, but I'm still not able to get it working.

My js call is:

var data = { value: 7 };
$http.post('api/controller/method', data);

But in fiddler there is no Content-Type and no JSON data. I want the Content-Type to be 'application/json' which should be default, right?

Thanks

o..o
  • 1,789
  • 5
  • 31
  • 59

2 Answers2

1
var data = { value: 7 };
$http({
        url: "api/controller/method",
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: $.param(data)
        }).success(function(data, status, headers, config) {
            //some code when success post
            }).error(function(data, status, headers, config) {
            //some code when error post
        });
  • Helped, thanks. But why the urlencoded is working instead of applicaion/json? – o..o May 27 '16 at 15:03
  • And it will not work when the simple value like 7 will be sent. – o..o May 27 '16 at 15:07
  • This [link](http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data) answer your cuestion about the **urlencoded** @matt-bridges – Rodrigo Garcia May 27 '16 at 15:12
  • And why wil not work with the simple value?, You have to check in the back end what parameters you are receiving and give an response – Rodrigo Garcia May 27 '16 at 15:14
  • When I send just 7, I read 0 on server. When I wrap it into var post = { data: 7 }, I have the object with property data = 7 on the server. Simple value does not work to me.. I have solved it with generics on the server side, but I don't get it why.. – o..o May 30 '16 at 14:39
0

Try this:

var response = $http.post('api/controller/method', data);
response.success(function(data, status1, headers, config) {
    //
}
Raghav Rangani
  • 843
  • 7
  • 23