繁体   English   中英

可能未处理的拒绝

[英]Possibly unhandled rejection

我正在尝试使用json-data调用PUT方法。 但是,当我单击toggle_save()按钮修改数据时,出现404 error和未处理的拒绝。

HTML:

<div ng-show="contact" id="contacts_edit()">
<div>
 <button class="btn btn-secondary" ng-hide="editMode" ng-click="toggle_edit()"> Edit </button>
 <button class="btn btn-success" ng-show="editMode" ng-click="toggle_save(cts.selectedcontact)"> Save </button>         
</div>

<b> Street:</b> 
    <p ng-hide="editMode">      
    {{cts.selectedcontact.location.street}} 
  </p>
<input type="text" ng-show="editMode" ng-model="cts.selectedcontact.location.street"><br/>

AngularJS:

$scope.toggle_save = function(contacts, id) {
  $scope.selectedcontact = [];

  $http({
    method: 'PUT',
    url: 'http://localhost:3000/contacts/' + contacts.editmode,
    data: {
      selectedcontact: contacts.selectedcontact
    }
  }).then(function(data) {
    console.log(data)
    $scope.cts.selectedcontact = data;
  })
}

您可以在catch块中处理未处理的拒绝。 由于API调用中的某些错误,您的API失败了。 确保联系人数据及其属性正确。

HTML

<button class="btn btn-success" ng-show="editMode" ng-click="toggle_save(cts.selectedcontact)"> Save </button> 

JS

 $scope.toggle_save = function(contacts) {
    $scope.selectedcontact = [];
    $http({
        method: 'PUT',
        url: 'http://localhost:3000/contacts/' + contacts.editmode,
        data: {
            selectedcontact: contacts.selectedcontact
        }
    }).then(function(data) {
        console.log(data)
        $scope.cts.selectedcontact = data.data;
    }).catch(function(data) {
       //handle rejections here
    });
}

404错误即将到来,因为API无法访问URL。 最简单的测试方法是将URL复制并粘贴到浏览器中。
未处理的拒绝可以通过在.then()之后放置.catch()来解决。 拒绝将作为参数传递给它。 您可以进行控制台或采取适当的措施。

 $scope.toggle_save = function(contacts) {
    $scope.selectedcontact = [];
        $http({
            method: 'PUT',
            url: 'http://localhost:3000/contacts/' + contacts.editmode,
            data: {
                selectedcontact: contacts.selectedcontact
            }
        }).then(function(data) {
            console.log(data)
            $scope.cts.selectedcontact = data.data;
        }).catch(function(data) {
           //handle rejections here
        });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM