2017-04-26 73 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 –

相關問題