我一直在从我创建的WCF服务应用程序中检索JSON数据时遇到一些问题。我有一个简单的WCF服务,它返回以下JSON:
[{"RoomId":1,"RoomName":"Big Room"},{"RoomId":2,"RoomName":"Medium Room"},{"RoomId":3,"RoomName":"Small Room"}]
我正在尝试使用JQuery从Web应用程序访问此数据。我尝试过各种JQuery语句来尝试检索数据,但没有出现任何内容。以下是我目前的最新尝试:
$(function () {
$.getJSON('http://localhost:6188/RoomBookingService.svc/GetRooms?callback=Rooms', function (data) {
alert(data);
});
以下是我的WCF服务应用程序代码:
[ServiceContract]
public interface IRoomBookingService
{
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetRooms")]
Room[] GetRooms();
配置:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RoomBookingServices.RoomBookingService" behaviorConfiguration="RoomBookingServiceBehaviour">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RoomBookingServices.IRoomBookingService" behaviorConfiguration="webHttpBehavior">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="RoomBookingServiceBehaviour">
<!-- 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="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
我需要JQuery做的就是返回JSON并将其显示在div标签或其他内容中,稍后它将被格式化。任何帮助都会很棒。
提前致谢。
答案 0 :(得分:2)
我相信在使用WCF的jsonp调用的典型用法中,回调参数被指定为问号。
$(function () {
$.getJSON('http://localhost:6188/RoomBookingService.svc/GetRooms?callback=?', function (data) {
alert(data);
});
然后jQuery getJSON方法将用动态生成的javascript函数替换问号以处理JSONP回调
See this article了解更多详情。