使用 Ajax Jquery 和 asmx 服务按 Id 获取数据
Getting Data By Id with Ajax Jquery and asmx service
我想通过 Id 获取选定的员工数据,因此将弹出一个模式并显示该 Id 的数据。 Id 解析为参数,但 asmx 服务最终不会触发。
这是客户端代码:
function getEmployeebyID(Id) {
$("#ModalTitle").html("Update Employee");
$("#MyModal").modal();
var Id = Id;
$.ajax({
url: "EmployeeService.asmx/GetEmployeeById",
type: "GET",
dataType: "json",
data: { Id: Id },
success: function (data) {
$('#txtFirstName').val(data[0].FirstName);
$('#txtLastName').val(data[0].LastName);
$('#txtGender').val(data[0].Gender);
$('#txtJobTitle').val(data[0].JobTitle);
$('#txtSalary').val(data[0].Salary);
$('#txtHireDate').val(data[0].HireDate);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}
这是服务器端代码:
[WebMethod]
public void GetEmployeeById(int Id)
{
Employee employee = new Employee();
string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetEmployeeById", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@Id";
parameter.Value = Id;
cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
employee.Id = Convert.ToInt32(rdr["Id"]);
employee.FirstName = rdr["FirstName"].ToString();
employee.LastName = rdr["LastName"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.JobTitle = rdr["JobTitle"].ToString();
employee.Salary = Convert.ToInt32(rdr["Salary"]);
employee.HireDate = Convert.ToDateTime(rdr["HireDate"]);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(employee));
}
当我在 运行 时调用数据时,服务器端代码正在工作,所以问题出在客户端并抛出: url 的请求格式无法识别,意外以 /GetEmployeeById 结尾
就我检查的元素而言,我看到了这个错误:localhost:50791/Services/EmployeeService.asmx/GetEmployeeById?Id=42 500(内部服务器错误)。无法弄清楚发生了什么......
试试这个
function getEmployeebyID(Id) {
$("#ModalTitle").html("Update Employee");
$("#MyModal").modal();
var Emp={
Id = Id
}
$.ajax({
url: "../EmployeeService.asmx/GetEmployeeById",
type: "POST",
contentType : "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(Emp),
success: function (data) {
$('#txtFirstName').val(data[0].FirstName);
$('#txtLastName').val(data[0].LastName);
$('#txtGender').val(data[0].Gender);
$('#txtJobTitle').val(data[0].JobTitle);
$('#txtSalary').val(data[0].Salary);
$('#txtHireDate').val(data[0].HireDate);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}
终于从这个How do I fix a "Request format is unrecognized for URL..." error in a web service running in IIS?
我添加了 web.config :
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
并从
中移除
$('#txtFirstName').val(data[0].FirstName);
[0]
一切正常。
我想通过 Id 获取选定的员工数据,因此将弹出一个模式并显示该 Id 的数据。 Id 解析为参数,但 asmx 服务最终不会触发。 这是客户端代码:
function getEmployeebyID(Id) {
$("#ModalTitle").html("Update Employee");
$("#MyModal").modal();
var Id = Id;
$.ajax({
url: "EmployeeService.asmx/GetEmployeeById",
type: "GET",
dataType: "json",
data: { Id: Id },
success: function (data) {
$('#txtFirstName').val(data[0].FirstName);
$('#txtLastName').val(data[0].LastName);
$('#txtGender').val(data[0].Gender);
$('#txtJobTitle').val(data[0].JobTitle);
$('#txtSalary').val(data[0].Salary);
$('#txtHireDate').val(data[0].HireDate);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}
这是服务器端代码:
[WebMethod]
public void GetEmployeeById(int Id)
{
Employee employee = new Employee();
string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetEmployeeById", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@Id";
parameter.Value = Id;
cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
employee.Id = Convert.ToInt32(rdr["Id"]);
employee.FirstName = rdr["FirstName"].ToString();
employee.LastName = rdr["LastName"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.JobTitle = rdr["JobTitle"].ToString();
employee.Salary = Convert.ToInt32(rdr["Salary"]);
employee.HireDate = Convert.ToDateTime(rdr["HireDate"]);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(employee));
}
当我在 运行 时调用数据时,服务器端代码正在工作,所以问题出在客户端并抛出: url 的请求格式无法识别,意外以 /GetEmployeeById 结尾 就我检查的元素而言,我看到了这个错误:localhost:50791/Services/EmployeeService.asmx/GetEmployeeById?Id=42 500(内部服务器错误)。无法弄清楚发生了什么......
试试这个
function getEmployeebyID(Id) {
$("#ModalTitle").html("Update Employee");
$("#MyModal").modal();
var Emp={
Id = Id
}
$.ajax({
url: "../EmployeeService.asmx/GetEmployeeById",
type: "POST",
contentType : "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(Emp),
success: function (data) {
$('#txtFirstName').val(data[0].FirstName);
$('#txtLastName').val(data[0].LastName);
$('#txtGender').val(data[0].Gender);
$('#txtJobTitle').val(data[0].JobTitle);
$('#txtSalary').val(data[0].Salary);
$('#txtHireDate').val(data[0].HireDate);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}
终于从这个How do I fix a "Request format is unrecognized for URL..." error in a web service running in IIS? 我添加了 web.config :
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
并从
中移除 $('#txtFirstName').val(data[0].FirstName);
[0]
一切正常。