Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http.post how to set the json data into request body

Tags:

angularjs

I am trying to send the post request with json data to server. But seems angularJS $http.post method does not set the data into body. How can I make it set the data into body?

The remote server is implemented use asp.net webapi and will read the data from body. so I need to set the json data into request body.

How can I implement this please? Thanks.

If the request send to same site, then it works. But if I send the request to CROS server, it does not work.

In the remote backend server, I already updated the webconfig to make it support CROS call, but it still does not work.

 $http.post(resourceUri, requestData)
    .success(function (response) {
    })
    .error(function (data, status, header, config) {
    });
like image 745
jamie2015 Avatar asked Mar 26 '15 07:03

jamie2015


2 Answers

You could build the request like this:

var req = {
 method: 'POST',
 url: 'http://example.com',
 headers: {
   'Content-Type': "application/json"
 },
 data: { test: 'test' }
}

$http(req).success(function(){...}).error(function(){...});
like image 190
Sohan Soni Avatar answered Oct 14 '22 00:10

Sohan Soni


YOu can do it as follows 1. make a controller 2. make a add data to the call

syntax for the post call is as follows

$http.post(url, data, [config])

app.controller('controller', function ($scope, $http, $routeParams) {
        $http.post('url',data.call1($routeParams.id))
            .success(function (response) {
                 $scope.response = response;
            })
            .error(function (data, status, headers, config) {
            });
    });


var data = {
    call1:
        function (value) {
            return {'key': value, 'key': 'some text'};
         }
}
like image 21
Ekansh Rastogi Avatar answered Oct 13 '22 23:10

Ekansh Rastogi