数据应该被插入到数据库中,但是它没有。JQuery在任何进程失败前警告动作。我认为url根本没有向控制器发送请求,谁能告诉我这个代码中的错误在哪里?
public class EmployeeController : ApiController {
static EmpRepository repository = new EmpRepository();
public string AddEmployee(Employee em) {
var response = repository.AddEmployees(em);
return response;
}
}
Jquery: function AddEmp() {
var Emp = {};
Emp.FirstName = $("#fname").val();
Emp.LastName = $("#lname").val();
Emp.Company = $("#company").val();
$.ajax({
url = 'api/AddEmployee';
tpye: "post",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(Emp),
dataType: "JSON",
success: function (response) {
alert(response);
},
error: function(x, e){
alert('Failed');
alert(x.response);
//alert(x.status);
}
});
}
$(document).ready(function () {
$("#save").click(function (e){
AddEmp();
e.preventDefault();
});
});
function AddEmp() {
var Emp = {};
url = 'api/AddEmployee';
Emp.FirstName = $("#fname").val();
Emp.LastName = $("#lname").val();
Emp.Company = $("#company").val();
$.ajax({
tpye: "post",
url:url,
contentType: "spplicstion/json;charset=utf-8",
data: JSON.stringify(Emp),
dataType: "JSON",
success: function (response) {
alert(response);
},
error: function(x, e){
alert('Failed');
alert(x.response);
//alert(x.status);
}
});
}
$(document).ready(function ()
{
$("#save").click(function (e){
AddEmp();
e.preventDefault();
});
经过一个小时的调试和测试,在url中发现一个错误,它的格式不正确,正确的格式是url = 'api/Employee/AddEmployee';