2012-01-26 2 views
0

내가 만든 WCF 서비스 응용 프로그램에서 JSON 데이터를 가져 오는 데 문제가 발생했습니다. 내가 jQuery를 사용하여 웹 응용 프로그램에서이 데이터에 액세스하려고WCF에서 JQuery를 통해 JSONP에 액세스

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

: 나는 다음과 같은 JSON을 반환하는 간단한 WCF 서비스가 있습니다. 데이터를 시도하고 검색하기 위해 다양한 JQuery 문을 시도했지만 아무것도 표시되지 않습니다.

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

는 IS 다음 내 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

WCF를 사용한 jsonp 호출의 일반적인 용도에서 콜백 매개 변수가 물음표로 할당된다는 것을 알았습니다.

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

나서 JQuery와해서 getJSON 방법은 상기 내용은 JSONP 콜백을

See this article를 처리하는 동적으로 생성 된 자바 스크립트 함수 물음표를 대체한다.

+0

아, 전에는 그랬지만, 어떤 이유로 그것을 꺼 냈습니다. 그것은 그것을 정리했다, Joe에게 감사한다! – moikey

+0

기꺼이 도와 줘서 기쁘다. –

관련 문제