Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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
使用Html/Javascript使用WCF服务_Javascript_Vb.net_Wcf_Web Services - Fatal编程技术网

使用Html/Javascript使用WCF服务

使用Html/Javascript使用WCF服务,javascript,vb.net,wcf,web-services,Javascript,Vb.net,Wcf,Web Services,文件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

文件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
导入系统
导入System.ServiceModel
公共接口IWebService
函数InsertReport(ByVal名称作为字符串)作为字符串
端接口
文件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应用程序中使用它。这个web应用程序使用jquery,只是一个标准的HTML5文档。我见过很多例子,人们用javascript或AJAX等调用WCF服务。。。我正在寻找一些可以实现这一点的代码,以及允许这类消费所需的附加web.config修改(和/或对我的服务进行常规更改)。

如果您的服务运行良好,那么您不需要在服务器端进行任何更改。web服务的功能独立于调用客户端。您可以使用一些工具测试您的服务,如

下一个HTML代码片段作为服务的客户端应该可以正常工作,使post URL适合您的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>

$(文档).ready(函数(){
$(“呼叫服务”)。单击(功能(e){
e、 预防默认值();
$.ajax({
键入:“POST”,
数据:“{”name:“+$(“输入#名称”).val()+“}”,
网址:'http://targetURL/Hello',
contentType:'application/json;charset=utf-8',
数据类型:“json”,
成功:
函数(数据、textStatus、XMLHttpRequest){
警报(数据d);
},
错误:
函数(XMLHttpRequest、textStatus、ErrorSwink){
警报(文本状态);
}
});
});
});    

希望我能帮忙

嗯,当我在SoapUI中发送请求时,服务似乎运行得很好。然而,我还不能让你的代码正常工作。我已经替换了URL,我已经将contentType更改为成功请求后在SoapUI中看到的内容。我还将数据类型更改为xml,这正是我想要的。SoapUI能告诉我更多关于我需要做什么来形成一个适当的请求吗?(我也已经在使用jquery 1.10.1以及jquery mobile 1.4.0,如果有必要的话)我得到了很多不同的答案。。。400个错误的请求似乎是我目前的关键。我尝试过添加SOAPAction之类的东西,但仍然没有成功。记录在案的是我一直在使用的(假的;))URL:--是这样吗?我还尝试了在末尾附加“?wsdl”的URL。我也尝试过添加“/MethodName”。您是否尝试过将客户端发送的数据更改为简单字符串或XML?正如我现在的代码中一样,它是json…我已经尝试过了。所以莱米问你这个。。。使用您提供的客户端代码。。你自己有工作版本吗?如果是这样的话,您能否提供您的service/interface/web.config代码以便我们进行比较?