2015-12-03 76 views
0

我一直試圖使用離子angularjs中的$ http請求連接到後端服務器。我想要使​​用API​​註冊一個用戶。有人能告訴我我的代碼有什麼問題嗎?這是我的controller.js如何使用angularjs中的API連接到後端服務器

angular.module('app.Auth', []) 

.factory('Auth', ['$q', '$http', '$localStorage', 'URL', function($q, $http, $localStorage, URL) { 

return { 
    register: function(data, success, error) { 
     $http.post(URL.api + 'register', data).success(success).error(error) 
    } 
}; 
}]) 
.controller('SignupCtrl', function($scope, $rootScope, $timeout, $stateParams, ionicMaterialInk,Auth,$localStorage,$state,Auth,URL) { 

$scope.signupError = function(msg){ 
    if(msg.status == false){ 
     $scope.errors = msg.errors; 
    }else{ 
     $scope.errors = ['Error accessing API.']; 
    } 
}; 


$scope.signupSuccess = function(response){ 
    if(response.status==false){ 
     $scope.errors = response.errors; 
    }else{ 
     alert("success!"); 
    } 

}; 

$scope.user = { 
    firstname:'', 
    middlename:'', 
    lastname:'', 
    password: '', 
    email: '' 
}; 

$scope.signupEmail = function(){ 
    Auth.register($scope.user,$scope.signupSuccess,$scope.signupError); 
}; 
}); 

,這是我signup.html

<ion-view view-title="Sign Up" name="signup"> 
<ion-content> 
    <div class="list"> 
    <div ng-if="errors.length > 0" class="error_bg" ng-repeat="error in errors"> 
      <p class="error" ng-bind="error"></p> 
     </div> 
    <label class="item item-input item-floating-label"> 
     <span class="input-label">First Name</span> 
     <input type="text" placeholder="First Name" style="color:purple; font-size:18px" ng-model="user.firstname"> 
    </label> 
    <label class="item item-input item-floating-label"> 
     <span class="input-label">Middle Name</span> 
     <input type="text" placeholder="Middle Name" style="color:purple; font-size:18px" ng-model="user.middlename"> 
    </label> 
    <label class="item item-input item-floating-label"> 
     <span class="input-label">Last Name</span> 
     <input type="text" placeholder="Last Name" style="color:purple; font-size:18px" ng-model="user.lastname"> 
    </label> 
    <label class="item item-input item-floating-label"> 
     <span class="input-label">Email Address</span> 
     <input type="text" placeholder="Email Address" style="color:purple; font-size:18px" ng-model="user.email"> 
    </label> 
    <label class="item item-input item-floating-label"> 
     <span class="input-label">Password</span> 
     <input type="password" placeholder="Password" style="color:purple; font-size:18px" ng-model="user.password"> 
    </label> 
    </div> 
    <button class="button button-block button-royal" style="background-color:blue" ng-click="signupEmail()">Continue</button> 

+0

您將不得不提供更多信息。你遇到什麼問題?如果你只是不知道如何使用$ http,那麼有很多例子。如果您遇到錯誤,請發佈錯誤和導致錯誤的代碼。 –

+0

好的我已經添加了詳細信息:) –

+0

目前還不清楚你的問題是什麼。你有錯誤嗎? –

回答

0

注重$http.post權sintax。

事實上,$http.post()返回承諾它具有方法then()有兩個參數(成功的情況和錯誤處理程序),所以要註冊服務的HTTP請求,應當自改寫:

$http.post(URL.api + 'register', data).success(success).error(error); 

到:

$http.post(URL.api + 'register', data).then(success, error); 

對於角承諾見https://docs.angularjs.org/api/ng/service/ $ q

+0

好吧,現在我正在使用它,但它返回錯誤 $ http.post(url, ; },功能errorCallback(響應){ \t \t警報( '錯誤') \t \t警報(response.data); }); –

+0

你會得到哪種錯誤? – beaver

+0

我沒有得到任何數據。我在repsonse中得到空值 –

相關問題