如何使用angularjs中的API连接到后端服务器
我一直在尝试使用ionic angularjs中的$http请求连接到后端服务器。我想注册一个使用API的用户。有人能告诉我我的代码有什么问题吗?这是我的controller.js如何使用angularjs中的API连接到后端服务器,angularjs,http,ionic,Angularjs,Http,Ionic,我一直在尝试使用ionic angularjs中的$http请求连接到后端服务器。我想注册一个使用API的用户。有人能告诉我我的代码有什么问题吗?这是我的controller.js angular.module('app.Auth', []) .factory('Auth', ['$q', '$http', '$localStorage', 'URL', function($q, $http, $localStorage, URL) { return { register: func
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>
名字
中名
姓
电子邮件地址
密码
继续
注意右边的税收 事实上,
$http.post()
返回一个承诺,该承诺包含带有两个参数(成功和错误处理程序)的方法then()
,因此注册服务的http请求应该从以下地址重写:
$http.post(URL.api + 'register', data).success(success).error(error);
致:
有关Angular中的承诺,请参见$q,您必须提供更多信息。你遇到了什么问题?如果您只是不知道如何使用$http,那么有很多例子。如果您遇到错误,请发布错误和导致错误的代码。好的,我已经添加了详细信息:)仍然不清楚您的问题是什么。你有错误吗?我只是想知道我做对了吗?因为我没有收到成功警报。或者有其他方法可以做到这一点吗?好的,现在我正在使用它,但它返回错误$http.post(url,{params:{first_name:'zeeshan'}})。然后(函数successCallback(response){alert(response.data);},函数errorCallback(response){alert('error')alert(response.data);});我没有得到任何数据。我在repsonseSo中获取null“response”或“response.data”是否为null?那么控制台日志(Chrome)“网络”选项卡呢?http状态代码和服务器的响应是什么?
$http.post(URL.api + 'register', data).then(success, error);