我正在尝试让我的 jquery 代码与 WCF 一起工作。我从一些非常简单的事情开始。WCF 服务托管在 IIS 上。它有一个简单的接口和一个简单的实现。以下是我的 WCF 代码。
[ServiceContract]
public interface IEvalService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetId",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetId();
}
上述接口的实现如下:
[AspNetCompatibilityRequirements(RequirementsMode=
AspNetCompatibilityRequirementsMode.Allowed)]
public class EvalService: IEvalService
{
public string GetId()
{
BenchMarkStatus benchMarkStatus = new BenchMarkStatus();
return benchMarkstatus.Name;
}
}
当然还有一个benchMark类如下:
[DataContract]
public class BenchMarkStatus
{
private string _name;
public BenchMarkStatus()
{
_name = "Sudarshan";
}
[DataMember]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
Web服务的配置文件如下:
<services>
<service name="BenchMarkServiceLiabrary.EvalService">
<endpoint address="10.66.26.171"
binding="webHttpBinding"
contract="BenchMarkServiceLiabrary.IEvalService"
behaviorConfiguration="WEB" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WEB">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true" />
在客户端,我有以下代码:
$.ajax({
type: 'GET',
url: "http://10.66.26.171/EvalService/Eval.svc/GetId?callback=?",
contentType: "json/application; charset=utf-8",
dataType: "jsonp",
jsonpCallback: 'jsonCallback',
success: function (data) { alert(data) }
});
function jsonCallback() {
alert("me");
}
我收到 404 错误。最初我遇到了跨域问题......但现在无法从客户端代码访问 GetId 函数。有人可以帮忙吗?