javascript – AngularJS $http.post with body

我有Cordova和AngularJS的应用程序.

使用Angular,我将一个值发送到我的后端应用程序(Spring REST).我用它的方法是$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);
           });
};

我使用以下参数:

url:http://< location>:< port> /< application> /< web_socket_url>

数据:

data: {
        {
        "incidentTypeId": 5,"priorityId": 1,"creationDate": 1449676871234,"userId": 1,"latitude": 

我使用’data’而不是’params’,因为我想通过正文发送数据.我得到了这个与PUT功能一起使用,与此功能没有多大区别.

我的REST控制器

 @RequestMapping(value = "/employee/new",method = RequestMethod.POST)
    public NewEmployee createIncident(@RequestBody NewEmployee employee) {

    return employee;
}

我的NewEmployee模型

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":

我收到:

{  
     employeeId: 0
     priorityId: 0
     creationDate: null
     userId: 0
     information: null
     latitude: 0
     longitude: 0   
}

邮差

我用PostMan(谷歌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',data: JSON.stringify(data)
})
.then(function (success) {
  callback(success);
},function (error) {
  errorCallback(error.data);
});

哪里

data = {
     "employeeId":5,"longitude":

Further reading…

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...