2012-01-26 4 views
0

私が作成したWCFサービスアプリケーションからJSONデータを取得する際にいくつかの問題がありました。私はjQueryのを使用してWebアプリケーションからこのデータにアクセスしようとしていますWCFからJQuery経由でJSONPにアクセスする

[{"RoomId":1,"RoomName":"Big Room"},{"RoomId":2,"RoomName":"Medium Room"},{"RoomId":3,"RoomName":"Small Room"}] 

:私は、次のJSONを返す単純なWCFサービスを提供しています。私はさまざまなJQueryステートメントを試してデータを取得しようとしましたが、何も表示されません。私はjQueryのは、やらなければならないことが戻らある

<?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> 

[ServiceContract] 
public interface IRoomBookingService 
{ 
    [OperationContract] 
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetRooms")] 

    Room[] GetRooms(); 

設定:次は私のWCFサービスアプリケーションのコードである

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

:次は私の最新の試みでありますJSONを作成し、divタグなどで表示します。後でフォーマットされます。どんな助けも素晴らしいだろう。

ありがとうございます。

+1

http:// localhost:6188/RoomBookingService.svc/GetRooms?callback =返信内容 – Luke

+0

ここで問題は何ですか? – mowwwalker

答えて

2

私は、WCFを使用したjsonp呼び出しの一般的な使用法では、コールバックパラメータが疑問符として割り当てられることを覚えておいてください。

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

そして、jQueryのgetJSONメソッドは、JSONPコールバックに更なる詳細については、

See this articleを処理するために、動的に生成されるJavaScript関数で疑問符に置き換えられます。

+0

ああ、私は前にそれを持っていたが、何らかの理由でそれを取り出した。それはそれをクリアしました、ありがとうジョー! – moikey

+0

助けてくれることを嬉しく思います。 –

関連する問題