数据假定要插入数据库,但不是。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();
});
});
经过一小时的调试和测试,在url中发现错误,该错误的格式不正确,正确的格式为 url = 'api/Employee/AddEmployee';
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句