C# 从WFC服务返回Json结果并在$.ajax中使用结果
我有一个asp.net页面,我正在使用jQueryAjax。我需要创建一个可以在$.ajax中用作url的方法。我在网上搜索了一下,发现我需要创建WCF服务。我的解决方案是asp.NET3.5。我在IJsonDataService.cs接口中创建了两个方法,如下所示C# 从WFC服务返回Json结果并在$.ajax中使用结果,c#,asp.net,wcf,web-services,jquery,C#,Asp.net,Wcf,Web Services,Jquery,我有一个asp.net页面,我正在使用jQueryAjax。我需要创建一个可以在$.ajax中用作url的方法。我在网上搜索了一下,发现我需要创建WCF服务。我的解决方案是asp.NET3.5。我在IJsonDataService.cs接口中创建了两个方法,如下所示 [ServiceContract] public interface IJsonDataService { [OperationContract] Person DoWork(
[ServiceContract]
public interface IJsonDataService
{
[OperationContract]
Person DoWork();
[OperationContract]
string GetData();
}
and then in class file I have implemented them like this:
[WebGet(RequestFormat= WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json,UriTemplate="data/{id}")]
public Person DoWork(){
return new Person();
}
我的web.config如下所示:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="JsonDataServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="JsonDataServiceBehavior" name="JsonDataService">
<endpoint address="" binding="wsHttpBinding" contract="IJsonDataService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
我想将Id传递给这些方法,然后查询数据库并返回结果,然后将结果格式化为json并将该json返回到$.ajax方法。我需要在上述方法中做什么更改?如何将数据作为json返回,然后在$.ajax中使用它?请提出建议。以下是我发现有用的两篇文章教程 这个是最有用的
您可以检查一下。这种方法与您想要完成的类似
如果您想要一个示例JSONP,请参见您的配置需要如下所示:
<system.serviceModel>
<endpointBehaviors>
<behavior name="web">
<webHttp />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
<behavior name="json">
<enableWebScript />
</behavior>
</endpointBehaviors>
<behaviors>
<serviceBehaviors>
<behavior name="JsonDataServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="JsonDataServiceBehavior" name="JsonDataService">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="namespace.IJsonDataService">
</endpoint>
</service>
</services>
</system.serviceModel>
为什么我的配置文件看起来与所有教程不同。当我创建WCF服务时,web.config不会自动更改吗?@robert我有asp.net web表单应用程序,而不是MVC,我需要json响应。这与MVC或Webform无关。这是关于使用ajax调用WebService的,这在跨域策略中是不允许的,但在该链接中我指出,只有使用JSONPy才有可能。您的端点绑定需要是webHttpBinding,而不是wsHttpBinding。此外,您还需要包含适当的serviceBehavior
<system.serviceModel>
<endpointBehaviors>
<behavior name="web">
<webHttp />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
<behavior name="json">
<enableWebScript />
</behavior>
</endpointBehaviors>
<behaviors>
<serviceBehaviors>
<behavior name="JsonDataServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="JsonDataServiceBehavior" name="JsonDataService">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="namespace.IJsonDataService">
</endpoint>
</service>
</services>
</system.serviceModel>