数据假设要插入数据库但不会。任何进程之前的JQuery警报失败操作。我认为网址根本不向控制器发送请求,有人能告诉我这段代码中的错误在哪里。
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();
});
});
答案 0 :(得分:1)
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();
});
答案 1 :(得分:1)
经过一小时的调试和测试后,在网址中发现错误,其格式不正确,格式正确为url = 'api/Employee/AddEmployee';