我总结几个关键点
1. 服务必须声明为ScriptService(否则会出现下面的问题)

2.服务的方法不需要任何更改,保持原状
3.客户端用jquery的.ajax方法来调用
3.1 type必须是post
3.2 contentType必须是application/json
3.3 dataType必须是json
4.默认返回的json都是用d为根键的

1、编写4种WebService方法

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]                             //令WebService成功传入Json参数,并以Json形式返回结果
    [GenerateScriptType(typeof(Person))]        //不是必要,但推荐添加(如果Person里面再嵌套另一个复杂类型,则必要声明)
    [ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {
        /// <summary>
        /// 无任何参数
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

/// <summary>
        /// 传入参数
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [WebMethod]
        public string Hello(string name)
        {
            return string.Format("Hello {0}", name);
        }

/// <summary>
        /// 返回泛型列表
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        [WebMethod]
        public List<int> CreateArray(int i)
        {
            List<int> list = new List<int>();

while (i >= 0)
            {
                list.Add(i--);
            }

return list;
        }

/// <summary>
        /// 返回复杂类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="age"></param>
        /// <returns></returns>
        [WebMethod]
        public Person GetPerson(string name, int age)
        {
            return new Person()
            {
                Name = name,
                Age = age
            };
        }
    }

/// <summary>
    /// 复杂类型
    /// </summary>
    public class Person
    {
        public string Name { get; set; }

public int Age { get; set; }
    }

2、编写js调用以上方法

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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 runat="server">
    <title>无标题页</title>
    <style type="text/css">
    input
    {
        width:200px;
    }
    </style>

<script type="text/javascript" src="jquery-1[1].2.6.min.js"></script>
    <script type="text/javascript">
    $(function(){  
      
        /*
            1、WebService请求类型都为Post,WebService的Url为“[WebServiceUrl]/[WebMethod]”
            2、contentType声明为Json
            3、data要用Json的字符串格式传入
            4、设置了dataType为json后,result就直接为返回的Json对象。

*/
        
        //调用无参数方法
        $("#btnHelloWorld").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/HelloWorld",
                data:"{}",
                dataType:'json',
                success:function(result){                    
                    alert(result.d);
                }
            });
        });        
        
        //传入1个参数
        $("#btnHello").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/Hello",
                data:"{name:'KiMoGiGi'}",
                dataType:'json',
                success:function(result){                    
                    alert(result.d);
                }
            });
        });
        
         //返回泛型列表
        $("#btnArray").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/CreateArray",
                data:"{i:10}",
                dataType:'json',
                success:function(result){                    
                    alert(result.d.join(" | "));
                }
            });
        });
        
         //返回复杂类型
        $("#btnPerson").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/GetPerson",
                data:"{name:'KiMoGiGi',age:26}",
                dataType:'json',
                success:function(result){
                    var person = result.d;
                    var showText = [];
                    for(var p in person){
                        showText.push(p + ":" + person[p]);
                    }
                    alert(showText.join("\r\n"));
                }
            });
        });
    });
    </script>
</head>
    <body>
        <form id="form1" runat="server">
            <p>
                <input type="button" id="btnHelloWorld" value="HelloWorld" />
            </p>
            <p>
                <input type="button" id="btnHello" value="Hello" />
            </p>
            <p>
                <input type="button" id="btnArray" value="CreateArray" />
            </p>
            <p>
                <input type="button" id="btnPerson" value="GetPerson" />
            </p>
        </form>
    </body>
</html>

Jquery调用webService的四种方法 转载-记录的更多相关文章

  1. Jquery调用webService的四种方法

    1.编写4种WebService方法 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(Conf ...

  2. php调用webservice的几种方法

    原文:php调用webservice的几种方法 1.WSDL模式: $soap = new SoapClient("http://192.168.6.69:8899/Service1.asm ...

  3. jQuery事件绑定的四种方法

    jQuery中提供了四种绑定事件的方法,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undelegate.off: 一.on()方法(首选方法) ...

  4. 通用的调用WebService的两种方法。(调用别人提供的wsdl)(转)

    转载自:http://blog.sina.com.cn/s/blog_65933e020101incz.html1.调用WebService的Client端采用jax-ws调用WebService:流 ...

  5. C#调用webService的几种方法

    转自: WebClient 用法小结 http://www.cnblogs.com/hfliyi/archive/2012/08/21/2649892.html http://www.cnblogs. ...

  6. asp.net远程调用WebService的两种方法(转载)

    一,静态方法在“解决方案‘项目名’” -> 相应的文件夹,如“Web References” ->右键“添加WEB引用”->在URL里写入地址.二,动态方法在“解决方案‘项目名’” ...

  7. Java开发WebService的几种方法--转载

    webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...

  8. Java中遍历map的四种方法 - 转载

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  9. java调用webservice接口 几种方法

    webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...

随机推荐

  1. linux中的ps命令用法。

    在linux中使用ps命令可以查看有哪些进程在运行和运行的状态.进程是否结束.进程有没有僵尸.哪些进程占用了过多的资源等等. ps命令最常用的是用于监控后台进程的工作情况. 名称:ps 使用权限:所有 ...

  2. XSS CSRF

    XSS CSRF XSS 参考 https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC ...

  3. Springmvc+Spring+Hibernate搭建方法及实例

    Springmvc+Spring+Hibernate搭建方法及实例  

  4. 我的 Azure VM 为何会重新启动?

    在客户创建的客服案件中, Azure VM意外重启是一个常见的问题,客户要求客服确定重新启动的原因.希望下面的详细说明能够帮助您了解 Azure VM重新启动的原因. WindowsAzure大约 ...

  5. VS2010/MFC对话框:向导对话框的创建及显示

    向导对话框的创建及显示 本节将为大家演示如何创建向导对话框. 仍然以前面的“加法计算器”的例子为基础,在其中加入向导对话框,我们可以用它来说明加法计算器的使用方法,一步一步引导用户操作,这也是比较常见 ...

  6. hdoj 2546 饭卡(0-1背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路分析:该问题为0-1背包问题的变形题:问题求余额最少,设开始的余额为V,则求得用V-5可以买 ...

  7. josscrowcroft / Simple-JavaScript-DOM-Inspector

    josscrowcroft / Simple-JavaScript-DOM-Inspector josscrowcroft / Simple-JavaScript-DOM-Inspector

  8. wordpress在window下完美实现301重定向的方法

    问题: 首先,简单说一下关于301重定向的问题,最简单的理解就是,假设你的主机上绑定有 www.uilike.cn, uilike.cn, www.uiseo.cn三个域名,当你想输入 uilike. ...

  9. 原生javascript实现ajax,post参数

    var json = { userid: userid, cid: cid, openid: openid, type: 1 }; // 原生ajax json = (function(obj){ / ...

  10. Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法

    安装apache2 sudo apt-get install apache2 安装完成,运行如下命令重启下: sudo /etc/init.d/apache2 restart 在浏览器里输入http: ...