简体   繁体   English

使用JavaScript消费Wcf服务

[英]Consuming Wcf Service using JavaScript

I have created a WCF Service. 我创建了WCF服务。 It is working fine. 一切正常。 When I am consuming it using Java Script, it can not find the Service. 当我使用Java脚本使用它时,它找不到服务。

My Java Script Code: 我的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>

My Service Contract: 我的服务合同:

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

All files are created in the same solution. 所有文件都在同一解决方案中创建。 Check it out: 看看这个:

在此处输入图片说明

My HTML Page: 我的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>

When i click on the Send Message button, the console says IDataService is not defined. 当我单击“发送消息”按钮时,控制台显示未定义IDataService。

在此处输入图片说明

My Web Config file: 我的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>

You have to configure end point in your web.config file I have checked it and it's working fine in my pc. 您必须在您检查过的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