加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

javascript – AngularJS $http.post with body

发布时间:2020-12-15 01:23:14 所属栏目:大数据 来源:网络整理
导读:我有Cordova和AngularJS的应用程序. 使用Angular,我将一个值发送到我的后端应用程序(Spring REST).我用它的方法是$http.post. 问题 当我尝试将数据发送到我的服务器时,Spring不会在我的实体中设置值.由于这个原因,我无法保存我的新数据. 角度代码 我的Angula

我有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…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读