我想使用基于REST的服务来输入数据。当我正在使用该服务时,我的服务没有被击中。在IE上使用网络图标时显示400错误。我的代码如下: -
//IService1.cs
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
void AddEmployee(string EmpName, int Salary, string Dept);
//Service1.svc.cs
public void AddEmployee(string EmpName, int Salary, string Dept)
{
//Employee emp = new Employee();
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Emp (sEmpName,iSalary,sDept) Values('" + EmpName + "','" + Salary + "','" + Dept + "') ", con);
cmd.ExecuteNonQuery();
con.Close();
}
在我的aspx页面上使用服务
function insertEmployeeWCF() {
var Name = $('#txt2').val();
var Salary = $('#txt3').val();
var Department = $('#txt4').val();
$.ajax({
type: "POST",
url: "http://localhost:1626/Service1.svc/AddEmployee",
//data: "{}",
data: "{'EmpName':'" + Name + "','Salary':'" + Salary + "','Dept':'" + Department + "'}",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (response) {
var employee = response.d;
$('#<%=lbl5.ClientID%>').html("Value successfully inserted")
}
});
}