簡體   English   中英

使用JavaScript消費Wcf服務

[英]Consuming Wcf Service using JavaScript

我創建了WCF服務。 一切正常。 當我使用Java腳本使用它時,它找不到服務。

我的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);
}

所有文件都在同一解決方案中創建。 看看這個:

在此處輸入圖片說明

我的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。

在此處輸入圖片說明

我的Web配置文件:

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

您必須在您檢查過的web.config文件中配置端點,並且在我的PC上運行正常。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM