Angularjs angular js,asp.net mvc:$http是否仅适用于ApiController,而不适用于普通控制器?
我正在尝试使用AngularJS1.*和asp.NETMVC5开发一个简单的表单。当我尝试使用$http()保存表单数据时,它会点击操作,但没有传递任何数据。但是,如果我使用$.post(),它将非常有效 谁能解释一下原因吗?这是因为我们需要使用ApiController而不是普通mvc的控制器吗?或者潜在的问题是什么 AddEmployeeController.jsAngularjs angular js,asp.net mvc:$http是否仅适用于ApiController,而不适用于普通控制器?,angularjs,asp.net-mvc-5,Angularjs,Asp.net Mvc 5,我正在尝试使用AngularJS1.*和asp.NETMVC5开发一个简单的表单。当我尝试使用$http()保存表单数据时,它会点击操作,但没有传递任何数据。但是,如果我使用$.post(),它将非常有效 谁能解释一下原因吗?这是因为我们需要使用ApiController而不是普通mvc的控制器吗?或者潜在的问题是什么 AddEmployeeController.js (function () { 'use strict'; angular.module('ApplicationModule'
(function () {
'use strict';
angular.module('ApplicationModule').controller('AddEmployeeController', function ($scope, SPACRUDService) {
$scope.Id = 0;
$scope.employee = {
FirstName: 'Hello',
MiddleName: '',
LastName: '',
CurrentAddress: '',
PermanentAddress: '',
Gender: 'M',
MobilePhone: '',
HomePhone: '',
Email: '',
CitizenshipNumber: '',
FatherName: 'n/a',
DOB: '',
TaxID: '',
EmergencyFullName: '',
EmergencyRelationship: '',
EmergencyPhoneNo: '',
EmergencyMobileNo: '',
IsCitizenshipCertProvided: false,
IsAcademicCertProvided: false,
IsExpOrReferenceLetterProvided: false
};
$scope.save = function () {
var Employee = $scope.employee;
var promisePost = SPACRUDService.post(Employee);
promisePost.then(function (data) {
alert("Saved Sucessfully!");
},
function (error) {
$scope.error = "Failed ", error;
}
);
};
});
AddEmployeeController.$inject = ['$location'];
function AddEmployeeController($location) {
/* jshint validthis:true */
var vm = this;
vm.title = 'AddEmployeeController';
activate();
function activate() { }
} })();
(function () {
'use strict';
angular.module('ApplicationModule').service("SPACRUDService", function ($http) {
//gets all employees
this.getEmployees = function () {
var request = $http({
method: "get",
url: "/Employee/GetAllEmployees",
});
return request;
}
//gets a employee by id
this.getEmployee = function (Id) {
var request = $http({
method: "get",
url: "/Employee/GetEmployeeById",
data: id
});
return request;
}
//creates a new employee
//this.post = function (Employee) {
// var request = $http({
// method: "post",
// url: "/Employee/Save",
// data: Employee
// });
// return request;
//}
//for creating a new employee
this.post = function (emp) {
// return $.post('/Employee/Save', emp); //this one works
return $http.post('/Employee/Save', emp); //this doesn't
}
//updates the chosen employee
this.put = function (Employee, Id) {
var request = $http({
method: "post",
url: "/Employee/Update",
data: { id: Id, employee: Employee }
});
return request;
}
//removes the employee
this.put = function (Id) {
var request = $http({
method: "post",
url: "/Employee/Delete",
data: id
});
return request;
}
}) })();
Service.js
(function () {
'use strict';
angular.module('ApplicationModule').controller('AddEmployeeController', function ($scope, SPACRUDService) {
$scope.Id = 0;
$scope.employee = {
FirstName: 'Hello',
MiddleName: '',
LastName: '',
CurrentAddress: '',
PermanentAddress: '',
Gender: 'M',
MobilePhone: '',
HomePhone: '',
Email: '',
CitizenshipNumber: '',
FatherName: 'n/a',
DOB: '',
TaxID: '',
EmergencyFullName: '',
EmergencyRelationship: '',
EmergencyPhoneNo: '',
EmergencyMobileNo: '',
IsCitizenshipCertProvided: false,
IsAcademicCertProvided: false,
IsExpOrReferenceLetterProvided: false
};
$scope.save = function () {
var Employee = $scope.employee;
var promisePost = SPACRUDService.post(Employee);
promisePost.then(function (data) {
alert("Saved Sucessfully!");
},
function (error) {
$scope.error = "Failed ", error;
}
);
};
});
AddEmployeeController.$inject = ['$location'];
function AddEmployeeController($location) {
/* jshint validthis:true */
var vm = this;
vm.title = 'AddEmployeeController';
activate();
function activate() { }
} })();
(function () {
'use strict';
angular.module('ApplicationModule').service("SPACRUDService", function ($http) {
//gets all employees
this.getEmployees = function () {
var request = $http({
method: "get",
url: "/Employee/GetAllEmployees",
});
return request;
}
//gets a employee by id
this.getEmployee = function (Id) {
var request = $http({
method: "get",
url: "/Employee/GetEmployeeById",
data: id
});
return request;
}
//creates a new employee
//this.post = function (Employee) {
// var request = $http({
// method: "post",
// url: "/Employee/Save",
// data: Employee
// });
// return request;
//}
//for creating a new employee
this.post = function (emp) {
// return $.post('/Employee/Save', emp); //this one works
return $http.post('/Employee/Save', emp); //this doesn't
}
//updates the chosen employee
this.put = function (Employee, Id) {
var request = $http({
method: "post",
url: "/Employee/Update",
data: { id: Id, employee: Employee }
});
return request;
}
//removes the employee
this.put = function (Id) {
var request = $http({
method: "post",
url: "/Employee/Delete",
data: id
});
return request;
}
}) })();
员工控制员
[HttpPost]
public JsonResult Save(Employee emp)
{
db.Add(emp);
return Json(db.SaveChanges());
}
新的.net核心mvc要求将
[FormBody]
作为Json数据附加到参数。此行为来自web api 2
[HttpPost]
public JsonResult Save([FormBody]Employee emp)
{
db.Add(emp);
return Json(db.SaveChanges());
}
您可以检查请求中发送的对象吗。在控制台浏览器->网络上,添加附加到post请求的数据。