【问题标题】:How to create $http delete req in angularjs如何在 angularjs 中创建 $http 删除请求
【发布时间】:2015-07-16 13:42:58
【问题描述】:

我想要的只是通过单击一个按钮来删除表格的一行。我喜欢这样的事情

  .controller('removeInput' , function($scope, $http) {
    $scope.remove = function(index){
      var inputId = {'inputId': $scope.inputId};
      $http.delete(baseUrl + '/input/' ,inputId, _auth)
      .success(function (){
        console.log('deleted');
      }).error(function(err){
        console.log(err);
      });
    };
  });

在 html 中是这样的

<tbody ng-controller = "input">
 <tr ng-repeat = "inputs in inputData">
  <td ng-model = "inputId">{{inputs.inputId}}</td>
  <td><img ng-src = "{{inputs.thumbnailUrl}}"/></td>
  <td>{{inputs.filename}}</td>
  <td></td>
  <td>{{inputs.updatedAt.date}}</td>
  <td ng-controller = "removeInput">
   <a class = "btn btn-option" ng-click = "remove(index)">
    <span class = "glyphicon glyphicon-remove"></span>
   </a>
  </td>
 </tr>
</tbody>

而且它不工作。请指出我哪里错了?

【问题讨论】:

  • 什么是_auth?它在显示的代码中未定义。 not working 也不是正确的问题陈述。当您在浏览器开发工具网络选项卡中检查实际请求时,您会看到什么?应该会找到很多线索
  • _auth 是包含标头的全局变量
  • 你不想remove(inputs.inputId)吗?
  • CORS 的标头配置都设置在这个 _auth 变量中
  • @Claies 是的,我想要什么

标签: html angularjs server angularjs-ng-click


【解决方案1】:

您在这里并没有真正传递正确的值。不清楚$scope.inputId 是什么,但它不是您要删除的项目的值。试试这个:

<a class = "btn btn-option" ng-click = "remove(inputs.inputId)">

在 JavaScript 中:

$scope.remove = function(index){
  $http.delete(baseUrl + '/input/' + index, _auth)
  ....

【讨论】:

  • 它在控制台中显示对象 {status: 401, message: "Given api-key is not authorized!"}
  • 或者你还需要对象形式的,让我再编辑一下javascript
  • 我想要带有 url 的 id。像这样的 '/input/6654' 将是要传递的 url
  • 我的代码和你的一样,但它仍然给出同样的错误
  • 让我尝试最后一次编辑它;您在此处的 cmets 中描述的不是您的函数之前所做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多