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

6
  • jsfiddle.net/bkUEu/458 Commented May 27, 2016 at 14:45
  • Still not working. It's sending OPTIONS verb instead POST (posting against WebApi2). When I compose a POST in fiddler, it is working though Commented May 27, 2016 at 14:54
  • Sounds like you are trying to do a cross-domain POST. The browser will send OPTIONS if you are doing a cross-domain request. Commented May 27, 2016 at 14:58
  • Yes, I'm. And it is allowed on the server. Commented May 27, 2016 at 15:03
  • Is the server responding to OPTIONS? The server should return Access-Control-Allow-Methods: POST. Commented May 27, 2016 at 15:07

2 Answers 2

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
        });
Sign up to request clarification or add additional context in comments.

5 Comments

Helped, thanks. But why the urlencoded is working instead of applicaion/json?
And it will not work when the simple value like 7 will be sent.
This link answer your cuestion about the urlencoded @matt-bridges
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
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..
0

Try this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.