Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JavaScript使用Wcf服务_C#_Javascript_Html_Wcf - Fatal编程技术网

C# 使用JavaScript使用Wcf服务

C# 使用JavaScript使用Wcf服务,c#,javascript,html,wcf,C#,Javascript,Html,Wcf,我已经创建了一个WCF服务。它工作得很好。当我使用Java脚本使用它时,它找不到服务 我的Java脚本代码: <head> <title></title> <script src="Scripts/jquery-2.1.1.js"></script> <script src="Scripts/MicrosoftAjax.js"></script> <script src="Service/DataServi

我已经创建了一个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>
所有文件都在同一解决方案中创建。请查看:

我的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文件中配置端点。我已经检查过了,它在我的电脑中工作正常

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


我已经详细检查了代码,代码没有问题,我想你的应用程序中缺少了一个js文件。我想我已经附加了所有文件。你必须在配置文件中配置端点。请参阅我的ansI已将我的配置文件放入。我不知道哪里出了问题。谢谢。让我看看你的密码。当我们访问html页面时,服务是否也必须在后台运行?在这个应用程序中,我们有inbulit web服务,因此当您运行应用程序时,服务将自动运行。在您的情况下,这是完美的运行。但是在我的例子中,我使用的是HTML页面,我知道它为什么没有运行。@NimitJoshi:没有,我已经使用了HTML页面,它也可以正常工作
 <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>