2014-02-13 2 views
0

FILE WebService.svc.vb는 HTML/자바 스크립트

Public Class WebService 
    Implements IWebService 
    Public Function HelloThere(ByVal name As String) As String Implements IWebService.HelloThere 
     Return "Hello there " & name 
    End Function 
End Class 

파일 IWebService.vb

Imports System 
Imports System.ServiceModel 

<ServiceContract()> 
Public Interface IWebService 

    <OperationContract()> 
    Function InsertReport(ByVal name As String) As String 
End Interface 

Web.Config 파일

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false 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="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="WebService"> 
     <endpoint address="WebService" binding="basicHttpBinding" contract="IWebService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 

     </service> 
    </services> 

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

FILE WebService.svc

를 사용하여 WCF 서비스를 소비
<%@ ServiceHost Language="VB" Debug="true" Service="WebService.WebService" CodeBehind="WebService.svc.vb" %> 

내 질문에 :이 서비스는 IIS 7에서 원격으로 호스팅됩니다 (.5 생각합니다). 웹 응용 프로그램에서이 서비스를 사용하고 싶습니다. 이 웹 응용 프로그램은 jquery를 사용하며 표준 HTML5 문서입니다. 사람들이 WCF 서비스를 자바 스크립트 또는 AJAX 등으로 호출하는 예제를 많이 보았습니다 ... 추가로 필요한 web.config (및/또는 일반적인 서비스 변경)뿐만 아니라이를 수행 할 몇 가지 코드를 찾고 있습니다.) 수정을 통해 이러한 유형의 소비를 허용합니다.

답변

1

서비스가 정상적으로 작동하면 서버 측에서 변경하지 않아도됩니다. 웹 서비스는 호출 클라이언트와 독립적으로 작동합니다. SoapUI과 같은 도구를 사용하여 서비스를 테스트 할 수 있습니다.

다음 HTML 스 니펫은 귀하의 서비스에 대한 클라이언트로서 올바르게 작동해야하며 귀하의 게시물 URL이 적합해야합니다. 보시다시피 json 데이터를 게시합니다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
     $("a#CallService").click(function (e) { 
      e.preventDefault(); 

      $.ajax({ 
      type: 'POST', 
      data: '{"name": "' + $("input#name").val() + '"}', 
      url: 'http://targetURL/Hello', 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      success: 
       function (data, textStatus, XMLHttpRequest) { 
       alert(data.d); 
       }, 
      error: 
       function (XMLHttpRequest, textStatus, errorThrown) { 
       alert(textStatus); 
       } 
      }); 
     }); 
     });  
    </script> 
</head> 
<body> 
    <input id="button" /><a id="CallService" href="#">Test</a> 
</body> 
</html> 

희망이 있습니다.

+0

SoapUI에서 요청을 보내면 서비스가 정상적으로 작동하는 것 같습니다. 그러나 아직 코드가 작동하지 않습니다. 나는 URL을 대체했고, 성공적인 요청 후에 SoapUI에 보이는 내용으로 contentType을 변경했다. 또한 dataType을 xml로 변경했습니다. 이는 원하는 것입니다. SoapUI가 적절한 요청을하기 위해 내가해야 할 일에 대해 더 많이 알려줄 수 있습니까? (jquery 모바일 1.4.0뿐만 아니라 jquery 1.10.1도 이미 사용하고 있습니다.) – JzInqXc9Dg

+0

무엇이 오류입니까 ??? –

+0

나는 여러가지 다른 것들을 얻었습니다 ... 400 나쁜 요청이 지금 내 바위처럼 보입니다. 나는 SOAPAction을 추가하는 것과 같은 것을 시도했다. 그러나 여전히 주사위는 없다. 여기 레코드 (가짜;)) URL을 사용하고 있습니다 : http : //somehost/service/WebService.svc - 그게 맞죠? 또한 끝에 "? wsdl"이 추가 된 URL을 시도했습니다. 나는 또한 "/ MethodName"을 덧붙였다. – JzInqXc9Dg