속성 ID, 이름, 사양을 가진 직원 컨트롤러가 있습니다. 저는 아약스 전화를 받고 직원 목록을 얻는 직원 서비스를 하나 만들었습니다. 하지만 매번 ''사용자가되고 있습니다. 내가 코드를 디버깅 할 때 먼저 성공을 호출 한 다음 Ajax 호출을 찾는다. 서비스없이 AJAX 호출을하면 제대로 작동합니다.AngularJS에서 서비스 아약스 전화하는 방법?
angular.module('EmployeeServiceModule', [])
.service('EmployeeSer', ['$http',function ($http) {
this.Users = '';
this.errors = '';
this.SearchEmployee = function() {
// Ajax call
$http({
method: 'GET',
url: '/Home/GetEmployeeList',
params: { filterData: 'Test' },
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(onSuccess, onError);
var onSuccess = function (response) {
this.userUsers = response.data;
this.errors = '';
};
var onError = function (reason) {
this.userUsers = reason;
this.errors = "Error in retrieving data.";
};
return this.Users;
}
}]);
angular.module('Employee', ['EmployeeServiceModule'])
.controller('EmployeeController', ['EmployeeSer', '$scope', '$http', function (EmployeeSer, $scope, $http) {
this.Id = '';
this.name = '';
this.expertise = '';
$scope.repoSortOrder = 'id';
$scope.filterField = '';
// Call to service
this.GetAllEmployee = function() {
// Initiates the AJAX call
$scope.User = EmployeeSer.SearchEmployee();
// Returns the promise - Contains result once request completes
return true;
};
this.AddEmployee = function() {
var empData = {
Id: $("#txtId").val(),
Name: $("#txtName").val(),
Expertise: $("#expertise").val()
};
$http({
method: 'POST',
url: '/Home/Create',
params: JSON.stringify(empData),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(onSuccess, onError);
// Returns the promise - Contains result once request completes
return true;
};
var onSuccess = function (response) {
$scope.user = response.data;
$scope.error = '';
};
var onError = function (reason) {
$scope.error = "Error in retrieving data.";
};
}]);
감사 ... 두 번째 작품이 여기 있습니다. –
예 두 번째 작품입니다. – rahul
헤더 : { 'Content-Type': 'application/x-www-form-urlencoded'}는 데이터와 함께 작동합니다. $ ('# formid'). serialize() – Zerubbabel