【发布时间】:2014-05-13 12:34:07
【问题描述】:
我正在尝试使用 JQuery 使用我的基于 REST 的 WCF 服务,但 Page 上没有发生任何事情。我还尝试了互联网上的各种链接。 请在下面找到我的代码:-
IService1.cs
namespace WcfRest
{
[ServiceContract]
public interface IService1
{
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Employee?id={id}")]
[OperationContract]
List<Employee> GetEmployeeDetails(int Id);
}
[DataContract]
public class Employee
{
[DataMember]
public int EmpId { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public int Salary { get; set; }
[DataMember]
public string Dept { get; set; }
}
}
Service1.svc.cs
namespace WcfRest
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=sa");
public List<Employee> GetEmployeeDetails(int Id)
{
Employee emp = new Employee();
con.Open();
SqlCommand cmd = new SqlCommand("Select Id,sEmpName,iSalary,sDept from Emp where Id='" + Id + "' ", con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
emp.EmpId = Convert.ToInt32(dr["Id"]);
emp.EmpName = dr["sEmpName"].ToString();
emp.Salary = Convert.ToInt32(dr["iSalary"]);
emp.Dept = dr["sDept"].ToString();
con.Close();
List<Employee> list = new List<Employee>();
list.Add(emp);
return list;
}
}
}
服务的web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfRest.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="WcfRest.IService1" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true" name=""/>
</webScriptEndpoint>
</standardEndpoints>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
ConsumeService.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
$(function () {
$('#btn1').click(getEmployeeWCF);
});
function getEmployeeWCF() {
$.ajax({
type: "POST",
url: "http://localhost:1626/Service1.svc/GetEmployeeDetails",
//data: "{}",
data: "{'Id':'" + $('#txt1').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var employee = response.d;
$('#output').empty();
$.each(employee, function (index, emp) {
$('#output').append('<p></strong><br /> Id: ' +
emp.Id + '<br />Name: ' +
emp.Name + '<br />Salary: £' +
emp.Salary + '<br />Department: ' +
emp.Department + '</p>');
});
},
failure: function (msg) {
$('#output').text(msg);
}
});
}
</script>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<input id="btn1" type="button" value="button" /></div>
</form>
网站的web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
</system.web>
</configuration>
请告诉我需要对我的代码进行哪些更改。 请帮忙 谢谢
【问题讨论】:
-
您在尝试上述代码时得到了什么?
-
是否出现任何错误?你试过调试吗?
-
尝试调试并检查方法是否在服务器端被调用?
-
我收到 404 错误 URL /Service1.svc/GetEmployeeDetails
-
@rahulaggarwal 你是否在 ajax 脚本之前包含了 jQuery 库?