2012-01-26 53 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

樂意幫忙,很高興它做到了。 –