【问题标题】:Consume WCF Service using Html/Javascript使用 Html/Javascript 使用 WCF 服务
【发布时间】:2014-02-13 16:02:15
【问题描述】:

文件 WebService.svc.vb

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>

文件 WebService.svc

<%@ ServiceHost Language="VB" Debug="true" Service="WebService.WebService" CodeBehind="WebService.svc.vb" %>

我的问题是:此服务远程托管在 IIS 7(我认为是 .5)中,我想在 Web 应用程序中使用它。这个网络应用程序使用 jquery,只是一个标准的 HTML5 文档。我已经看到很多例子,人们使用一些 javascript 或 AJAX 等调用 WCF 服务... ) 修改以允许这种类型的消费。

【问题讨论】:

    标签: javascript vb.net wcf web-services


    【解决方案1】:

    如果您的服务运行良好,那么您无需在服务器端进行任何更改。您的 Web 服务功能独立于调用客户端。您可以使用SoapUI 之类的工具来测试您的服务。

    下一个 HTML sn-p 应该可以作为您服务的客户端正常工作,并且帖子 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>
    

    希望我能帮上忙!

    【讨论】:

    • 好吧,当我在 SoapUI 中发送请求时,服务似乎工作得很好。但是,我还不能让您的代码正常工作。我已替换 URL,我已将 contentType 更改为成功请求后在 SoapUI 中看到的内容。我还将 dataType 更改为 xml,这正是我想要的。 SoapUI 可以告诉我更多关于我需要做什么来形成正确的请求吗? (如果重要的话,我也已经在使用 jquery 1.10.1 和 jquery mobile 1.4.0)
    • 我得到了很多不同的... 400 Bad Request 目前似乎是我的最爱。我已经尝试过添加 SOAPAction ..但仍然没有骰子。为了记录,这里是我一直在使用的(假的;))URL:somehost/service/WebService.svc——对吗?我还尝试了在末尾附加“?wsdl”的 URL。我也尝试过附加“/MethodName”。
    • 您是否尝试过将客户端发送的数据更改为简单字符串或 XML?因为它现在在我的代码中,它是 json...
    • 我试过了。所以lemmie问你这个......使用你提供的客户端代码......你自己有一个工作版本吗?如果是这样,您能否提供您的 service/interface/web.config 代码以便我们进行比较?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多