zoukankan      html  css  js  c++  java
  • Jquery调用本地WCF服务

    有两种方法可实现AJAX调用本地WCF服务,以下使用Jquery实现,具体流程如下:

    服务接口 IService1.cs:

    namespace wcftest
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string DoWork(string user);
        }
    }

    服务实现 Service1.svc:

    namespace wcftest
    {
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1 : IService1
        {
            string IService1.DoWork(string user)
            {
                return "hello"+user;
            }
        }
    }

    Web.config设置:

    <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="test">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
        <services>
          <service name="wcftest.Service1">
            <endpoint behaviorConfiguration="test" address="" binding="webHttpBinding" contract="wcftest.IService1">
              <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
          </service>
        </services>
      </system.serviceModel>

    前台调用代码:

    <script src="jquery-1.3.1.min.js" language="javascript" type="text/javascript"></script>
    <body>
        <div>
            <script type="text/javascript" language="javascript">
                function test() {
                    $.ajax({
                        type: 'POST',
                        url: '/Service1.svc/DoWork',
                        contentType: 'text/json',
                        data: '{"user":" Beautiful Gril"}',
                        success: function (msg) {
                            alert(msg);
                        }
                    });
                }
            </script>
            <input id="btnQueryDictionary" type="button" value="调用" onclick="test();" />
        </div>
    </body>

  • 相关阅读:
    xshell的安装及连接linux的使用方法
    linux中yum install 命令无效
    linux-centOS环境下安装jdk8
    centOS不显示ipv4地址的解决办法
    centOS开启和关闭防火墙
    java-分布式-索引
    java-网络通信-索引
    java-中间件
    java-框架-索引
    JVM-索引
  • 原文地址:https://www.cnblogs.com/silent2012/p/2341787.html
Copyright © 2011-2022 走看看