【问题标题】:Error on POST with Restangular使用 Restangular 进行 POST 错误
【发布时间】:2014-11-04 11:52:27
【问题描述】:

我想从前端应用程序向后端发送一个带有复杂 JSON 对象的 POST 请求。

后端

我有一个名为 NotificationService 的类和一个名为 writeNotification 的方法,如下所示:

@POST
@Path(SERVICE_PATH_WRITENOTIFICATION)
public Response writeNotification(@FormParam("notification") String notification)

前端侧

我发送这个:

var obj = [ {
    to : $scope.formComboDestinatari.listaDestinatari,
    subject : $scope.formOggetto,
    date : "xxx",
    body : $scope.formTesto,
    type : $scope.formComboTipologia
} ];

如果我使用这种语法:

Restangular.one('serverpath/writenotification').post(obj);

当我尝试发送请求时出现此错误:

500 internal server error

如果我使用这种语法:

Restangular.one('serverpath').post('writenotification', obj);

400 Bad request

我不明白我的错在哪里。

【问题讨论】:

    标签: java restangular


    【解决方案1】:

    解决了。

    我错了两次。

    第一个:

    obj var 不能有 [],因为它代表一个数组。

    所以定义 var 变为:

    var obj = {
        to : $scope.formComboDestinatari.listaDestinatari,
        subject : $scope.formOggetto,
        date : "xxx",
        body : $scope.formTesto,
        type : $scope.formComboTipologia
    };
    

    第二个:

    现在我以这种方式使用 $http 而不是 Restangular:

    $http({
        url : "pathserver/writenotification",
        method : "POST",
        data : obj,
        headers : {
            "Content-Type" : "application/json; charset=utf-8",
            "Accept" : "application/json"
        }
    }).success(function(data) {
        $scope.items = data;
        alert('Notifica inviata');
    }).error(function(data) {
        alert('Invio notifica fallito');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 2014-03-03
      • 2015-09-26
      • 1970-01-01
      • 2013-09-24
      • 2014-08-25
      • 2017-01-20
      相关资源
      最近更新 更多