使用JavaScript消费Wcf服务

时间:2014-06-02 10:21:28

标签: c# javascript html wcf

我已经创建了一个WCF服务。它工作正常。当我使用Java Script使用它时,它无法找到服务。

我的Java脚本代码:

<head>
<title></title>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/MicrosoftAjax.js"></script>
<script src="Service/DataService.svc/js"></script>
<script type="text/javascript">
    $(function () {
        $("#BtnSendMessage").click(
        function () {
            CallXmlDataService();
        });
    });

    function CallXmlDataService() {        

        var Data_Service = new IDataService();
        var UserName = document.getElementById("TxtName").value;
        var UserEmail = document.getElementById("TxtEmail").value;
        var UserMessage = document.getElementById("TxtMessage").value;
        Data_Service.InsertData(UserName, UserEmail, UserMessage);
        alert("Data Inserted Successfully");
    }
</script>
</head>

我的服务合同:

[ServiceContract]
public interface IDataService
{
    [OperationContract]
    void InsertData(string Name, string Email, string Message);
}

所有文件都在同一解决方案中创建。看看:

enter image description here

我的HTML页面:

<body>
<table id="TblExport">
    <tr>
        <td><input type="text" id="TxtName" /></td>
        <td><input type="text" id="TxtEmail" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <textarea id="TxtMessage"></textarea>
        </td>
    </tr>
    <tr>
        <td><input type="button" id="BtnSendMessage" value="SendMessage" /></td>
        <td><label id="LblErrorMessage"></label></td>
    </tr>
</table>
</body>

当我点击发送消息按钮时,控制台说没有定义IDataService。

enter image description here

我的网站配置文件:

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="XmlDataApplication.Service.XmlDataApplicationBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="XmlDataApplication.Service.DataService">
    <endpoint address="" behaviorConfiguration="XmlDataApplication.Service.XmlDataApplicationBehavior" binding="webHttpBinding" contract="XmlDataApplication.Service.IDataService"></endpoint>
  </service>
</services>

1 个答案:

答案 0 :(得分:0)

您必须在web.config文件中配置终点我已经检查过它并且它在我的电脑上正常工作。

 <system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="WCFJSproxyDemo.Service.WCFJSproxyDemoBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>   
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="WCFJSproxyDemo.Service.DataService">
    <!--<endpoint address="" behaviorConfiguration="WCFJSproxyDemo.Service.WCFJSproxyDemoBehavior" 
              binding="webHttpBinding" contract="WCFJSproxyDemo.Service.IDemoWCFService">
    </endpoint>-->
      <endpoint address="" behaviorConfiguration="WCFJSproxyDemo.Service.WCFJSproxyDemoBehavior"
             binding="webHttpBinding" contract="WCFJSproxyDemo.Service.IDataService">
      </endpoint>
  </service> 
</services>