2012-01-26 50 views
0

我一直有一些问题从我创建的WCF服务应用程序中检索JSON数据。我有一个返回以下JSON一个简单的WCF服务:从WCF通过JQuery访问JSONP

[{"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标签或其他内容中,稍后将对其进行格式化。任何帮助都会很棒。

在此先感谢。

+1

把这个放到浏览器窗口http:// localhost:6188/RoomBookingService.svc/GetRooms?callback =房间并看看它返回什么 – Luke

+0

虽然这里有什么问题? – mowwwalker

回答

2

我相信,在典型的jsonp使用WCF调用时,回调参数被指定为问号。

$(function() { 
    $.getJSON('http://localhost:6188/RoomBookingService.svc/GetRooms?callback=?', function (data) { 
     alert(data); 
}); 

然后了jQuery的getJSON方法将与一个动态产生的javascript函数来处理JSONP回调

See this article进一步的细节替换问号。

+0

啊,我之前有过,但是我出于某种原因拿掉了它。这已经清除了,谢谢乔! – moikey

+0

乐意帮忙,很高兴它做到了。 –