AngularJS $http.post with body
AngularJS $http.post with body
我有一个带有Cordova和AngularJS的应用程序。
使用Angular,我会向后端应用程序发送一个值(SpringREST)。我使用的方法是$http.post.
问题
当我尝试将数据发送到我的服务器时,Spring不会在我的实体中设置值。因此,我无法保存我的新数据。
角度代码
我的AngularJS代码如下:
httpService.createInBackend = function (url, data, callback, errorCallback) {
http.post(url, data)
.then(function (success) {
callback(success);
}, function (error) {
errorCallback(error.data);
});
};
我使用以下参数:
网址:http://<location>:<port>/<application>/<web_socket_url>
数据:
data: {
{
"incidentTypeId": 5,
"priorityId": 1,
"creationDate": 1449676871234,
"userId": 1,
"latitude": <some valid location>,
"longitude": <some valid location>
}
},
timeout: 4000
}
我使用"数据"而不是"params,因为我想通过正文发送数据。我用PUT函数实现了这一点,它与这个函数没有太大区别。
我的REST控制器
@RequestMapping(value = "/employee/new", method = RequestMethod.POST)
public NewEmployee createIncident(@RequestBody NewEmployee employee) {
return employee;
}
我的新员工模型
private int employeeId;
private int priorityId;
private String information;
private Date creationDate;
private int userId;
private float longitude;
private float latitude;
角度结果
当使用控制台时,我从这种方法中得到以下结果:
我发送:
{
"employeeId":5,
"priorityId":1,
"creationDate":1449677250732,
"userId":1,
"information": "hello world!",
"latitude":<some valid location>,
"longitude":<some valid location>
}
我收到:
{
employeeId: 0
priorityId: 0
creationDate: null
userId: 0
information: null
latitude: 0
longitude: 0
}
邮差
我在PostMan(Google chrome插件)上也尝试了同样的方法,这样我的代码就可以工作了。因此,我确实认为这是我的AngularJS代码的问题。
我试过
我已经尝试使用以下AngularJS调用:
$http({
method: 'POST',
url: url,
data: data,
timeout: 4000
}).then(function (success) {
callback(success);
}, function (error) {
errorCallback(error);
});
然而,这并没有改变结果。仍然只有空值。
有人知道我做错了什么吗
您是否尝试过不使用缩写版本的$http?我认为如果你使用之类的东西,你的代码会起作用
$http({
method: 'POST',
url: url,
data: JSON.stringify(data)
})
.then(function (success) {
callback(success);
}, function (error) {
errorCallback(error.data);
});
其中
data = {
"employeeId":5,
"priorityId":1,
"creationDate":1449677250732,
"userId":1,
"information": "hello world!",
"latitude":<some valid location>,
"longitude":<some valid location>
}
进一步阅读。。。
更新的代码
更改$http.post:后
httpService.createInBackend = function (url, data, callback, errorCallback) {
http.post(url, data)
.then(function (success) {
callback(success);
}, function (error) {
errorCallback(error.data);
});
};
到标准$http类型的角度:
$http({
method: 'POST',
url: url,
data: data
}).then(function (success) {
callback(success);
}, function (error) {
errorCallback(error);
});
我体内数据的发送仍然没有起作用。
解决方案
这种$http请求的标准方式的问题是它不接受像这样的对象
data: {
{
"incidentTypeId": 5,
"priorityId": 1,
"creationDate": 1449676871234,
"userId": 1,
"latitude": <some valid location>,
"longitude": <some valid location>
}
},
timeout: 4000
}
我不得不将其更改为:
var incidentInformation = {
incidentTypeId: $scope.selectedIncident.id,
priorityId: $scope.priorityId,
information: $scope.information,
creationDate: Date.now(),
userId: JSON.parse(localStorage.getItem('user')).id,
latitude: location.latitude,
longitude: location.longitude
};
并添加一个新行:
incidentInformation = JSON.stringify(incidentInformation);
我可以直接将此值设置为数据值:(超时必须以这种方式完成,并且不能在数据对象中设置)
$http({
method: 'POST',
url: url,
data: incidentInformation,
timeout: 4000
}).then(function (success) {
callback(success);
}, function (error) {
errorCallback(error);
});
有了这些更改后的代码,后端现在可以正确地接收数据,我的应用程序可以保存数据。
感谢Yoogeeks提出的标准方法。奇怪的是AngularJS$http。"某些方法"与标准方法不同
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- 不同页面背景的body标记上的角度ng样式
- contentWindow.document.body is null
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- jQuery e.stopPropagation Conflict With body onclick
- JQuery appendTo Div with ID instead of appendTo('body
- AngularJS $http.post with body
- Firefox CSS transitions with body overflow:hidden
- Creating email link with dynamically generated body with htm
- Https Post Request with body
- Javascript: $('body:contains("")') with ht