AngularJS $ http.post如何将json数据设置为请求正文 [英] AngularJS $http.post how to set the json data into request body
问题描述
我正在尝试将带有json数据的发布请求发送到服务器.但是似乎angularJS $ http.post方法没有将数据设置为主体.如何使其将数据设置为正文?
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?
远程服务器是使用asp.net webapi实现的,它将从正文中读取数据.所以我需要将json数据设置为请求正文.
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.
如果请求发送到同一站点,则它可以工作.但是,如果我将请求发送到CROS服务器,它将无法正常工作.
If the request send to same site, then it works. But if I send the request to CROS server, it does not work.
在远程后端服务器中,我已经更新了webconfig以使其支持CROS调用,但是它仍然无法正常工作.
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) {
});
推荐答案
您可以按照以下步骤进行操作 1.做一个控制器 2.向通话添加数据
YOu can do it as follows 1. make a controller 2. make a add data to the call
帖子调用的语法如下
$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'};
}
}
这篇关于AngularJS $ http.post如何将json数据设置为请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!