【问题标题】:Can't get Restangular remove() to send DELETE request without body无法让 Restangular remove() 发送没有正文的 DELETE 请求
【发布时间】:2014-10-02 17:01:42
【问题描述】:

我一直在开发一个 webapp,它在 localhost 上工作。但是,在部署到实时站点后,任何 DELETE 请求都会收到 400(错误请求)错误,响应为““DELETE”请求可能不包含正文”。查看请求,包含的有效负载是我要删除的对象的 ID。我从 Restangular 开发者Here 那里搜索并找到了我的问题的解决方案:

RestangularProvider.setRequestInterceptor(function(elem, operation) {
   if (operation === "remove") {
      return undefined;
   } 
   return elem;
})

但是这对我不起作用。我也尝试过返回 null 而不是 undefined,但请求仍然包含 ID 作为正文。我在 if 语句中放置了一条 console.log 消息,它显示在删除请求响应之前,据我所知,它正在正确捕获删除请求。这是我进行删除调用时的示例:

$scope.delete = function() {
    Restangular.one('graph', $scope.model.id).remove().then(function() {
        $location.path('/');
    });
};

我无法弄清楚是什么导致请求中包含正文,以及为什么 RequestInterceptor 没有解决问题。任何有关导致此问题的帮助或指示都会很棒。

【问题讨论】:

    标签: restangular


    【解决方案1】:

    您真正拥有的东西看起来应该可以工作。您可以尝试使用 addRequestInterceptor 或 addFullRequestInterceptor,而不是 setRequestInterceptor。 setRequestInterceptor 已被弃用,但它应该仍然有效。尝试切换它,让我知道这是否有效。

    【讨论】:

      【解决方案2】:

      addRequestInterceptor 为我工作。

       RestangularProvider.addRequestInterceptor(function(elem, operation, what, url) {
                  if (operation === "remove") {
                      return null;
                  }
                  return elem;
              });
      

      【讨论】:

        猜你喜欢
        • 2019-10-16
        • 2013-02-16
        • 2017-01-27
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2023-03-08
        • 2018-04-27
        • 2011-07-07
        相关资源
        最近更新 更多