AngularJS $ http.post如何将json数据设置为请求体

时间:2022-08-22 17:37:28

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?

我正在尝试将带有json数据的post请求发送到服务器。但似乎angularJS $ http.post方法没有将数据设置为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.

远程服务器使用asp.net webapi实现,并将从正文中读取数据。所以我需要将json数据设置为请求体。

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.

如果请求发送到同一站点,那么它可以工作。但是如果我将请求发送到CROS服务器,它就不起作用。

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

在远程后端服务器中,我已经更新了webconfig以使其支持CROS调用,但它仍然无效。

 $http.post(resourceUri, requestData)
    .success(function (response) {
    })
    .error(function (data, status, header, config) {
    });

2 个解决方案

#1


8  

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

你可以这样做1.制作一个控制器2.为呼叫添加数据

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'};
         }
}

#2


10  

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(){...});

#1


8  

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

你可以这样做1.制作一个控制器2.为呼叫添加数据

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'};
         }
}

#2


10  

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(){...});