2017-04-26 71 views
0

我使用node,express,mongoose作为后端和angular js作为前端。我正在使用表单发送发布请求。如何使用Angular JS获得POST请求的响应?

<div class="alert alert-success" ng-show="success"> 
    {{success}} 
</div> 

<div class="alert alert-danger" ng-show="error"> 
    {{error}} 
</div> 

    <form id="newCategory" ng-submit="submit()"> 
    <div class="row"> 
     <div class="col-md-4"> 
     {{mySubmit}} 
     <div class="form-group"> 
      <input id="name" name="name" type="text" 
      placeholder="Name" class="form-control input-md" 
      ng-model="catName"> 
     </div><!-- ./form-group --> 

     </div><!-- ./col --> 

     <div class="form-group"> 
     <input type="submit" name="submit" value="submit" class="btn btn-primary"> 
     </div><!-- ./form-group --> 

    </div><!-- ./row --> 
    </form> 

其操控:

fetchModule.controller('CatsNewCtrl', ['$scope', '$rootScope', 'Request', 
    function($scope, $rootScope, Request) { 
     // Root Scope 
     $rootScope.heading = 'Categories'; 
     $rootScope.heading2 = 'new'; 
     $rootScope.newLink = undefined; 

     $scope.catName = ""; 
     $scope.submit = function() { 
     Request.post('/category', { name : $scope.catName }) 
      .then(function(data) { 
      $scope.success = data.msg; 
      }) 
      .catch(function(status) { 
      $scope.error = "Error: " + status; 
      }); 
     return false; 
     }; 
    }]); 

的结果是什么,我不知道我要去哪里错了!该服务工作正常,实际上我能够控制台记录结果,但我无法将其设置为范围。

(function() { 
    let fetchModule = angular.module('fetchModule', []); 

    /** 
    * A Service for Requests. 
    */ 
    fetchModule.service('Request', ['$http', function($http) { 
    /** 
    * Get's content of a specific URL. 
    * @param {String} url - URL to GET. 
    * @return {Promise} Promise resolves with contents or rejects with an error. 
    */ 
    this.get = function(url) { 
     // Promise 
     return new Promise((resolve, reject) => { 
     $http.get(url) 
      .success(result => { 
      resolve(result); 
      }) 
      .error((err, status) => { 
      reject(err); 
      }); 
     }); 
    }; 

/** 
* Get's content of a specific URL. 
* @param {String} url - URL to GET. 
* @param {String} data - Data to post. 
* @return {Promise} Promise resolves with contents or rejects with an error. 
*/ 
this.post = function(url, data) { 
    // Promise 
    return new Promise((resolve, reject) => { 
    $http.post(url, data) 
     .success((data, status, headers, config) => { 
     resolve(data); 
     }) 
     .error((data, status, headers, config) => { 
     resolve(status); 
     }); 
    }); 
}; 

请帮助我只是一个角度的起动器。

+1

成功方法的回调有数据是不是你想要的? – user93

+0

这是!没有问题,但我无法看到警报的结果 –

+0

你可以console.log(data.message)并检查而不是data.msg? – Ved

回答

2

由于$http本身会返回承诺,因此您在使用反模式包装$httpPromise

此外,由于全局Promise在角度上下文之外,因此每当它改变角度时需要通知角度来运行摘要以更新视图。使用角度承诺,情况并非如此。

更改Request.post到:

this.post = function(url, data) { 
    return $http.post(url, data); 
}; 

在控制器出现在返回的对象持有的属性从服务器的数据data

$scope.submit = function() { 
    Request.post('/category', { name : $scope.catName }) 
     .then(function(resp) { 
     $scope.success = resp.data.msg; 
     }) 
     .catch(function(err) { 
     $scope.error = "Error: " + err.status; 
     }); 
    return false; 
    }; 

注意微小的差别在于$http方法.success().error()已弃用

+0

你的答案似乎是合理的,但它不工作兄弟!如果有帮助,我正在使用1.3.9角度。我改变了代码,就像你刚才说的那样只是返回了http.post()并使用了.then,catch和.success和错误,但没有运气! –

+0

现在我甚至取消了服务,并直接做出$ http直接输出相同的输出console.log作用于作用域不起作用 –

+0

哪个'then()'或'catch()'被触发? – charlietfl

0
scope.submit = function() { 
    $http({ 
     url:'/category', 
     method:'POST' 
     data:{ name : $scope.catName }, 
     headers: {'Content-Type': 'application/json'}, 
    }) 
     .then(function(resp) { 
     $scope.success = resp.data.msg; 
     },function(error){ 
     $scope.error = "Error: " + err.status;} 
    )}; 

不要返回false,因为它是一个回调函数。而不是像上面那样使用catch块使用“功能块”。

我改进了解决方案。如果它能够工作,您可以重构它以将重用代码移动到您的服务中。不要错过在您的控制器中注入$ http。

+0

试过这个兄弟不工作 –

+0

我有改进我的回答,也试试吧@Johnfoo –

相关问题