繁体   English   中英

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

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

我正在尝试将带有json数据的发布请求发送到服务器。 但是似乎angularJS $ http.post方法没有将数据设置为主体。 如何使其将数据设置为正文?

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

请问如何实施? 谢谢。

如果请求发送到同一站点,则它可以工作。 但是,如果我将请求发送到CROS服务器,它将无法正常工作。

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

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

您可以这样构建请求:

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

$http(req).success(function(){...}).error(function(){...});

您可以按以下步骤进行操作:1.制作控制器2.在通话中添加数据

调用的语法如下

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM