• jQuery之调用WebService


    前台:

    <script type="text/javascript">
        $(function () {
            $("#btn1").click(function () {           //不带参数
                $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/HelloWorld",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (json) { alert(json.d); },
                    error: function (error) {
                        alert("调用出错" + error.responseText);
                    }
                });
            });
            $("#btn2").click(function () {           //带参数
                $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/ReturnString",
                    data: "{s:'" + $("#txt").val() + "'}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (json) { alert(json.d); },
                    error: function (error) {
                        alert("调用出错" + error.responseText);
                    }
                });
            });
            $("#btn3").click(function () {           //对象
                $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/ReturnDomain",
                    data: "{}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (json) { alert(json.d.Name + json.d.Age); },
                    error: function (error) {
                        alert("调用出错" + error.responseText);
                    }
                });
            });
            $("#btn4").click(function () {           //对象集合
                $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/ReturnList",
                    data: "{}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (json) {
                        $.each(json.d, function (key, value) {
                            alert(value.Name + value.Age);
                        });
                    },
                    error: function (error) {
                        alert("调用出错" + error.responseText);
                    }
                });
            });
            //返回DataTable(XML)
            $('#btn5').click(function () {
                $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/ReturnXML",
                    data: "{}",
                    dataType: 'xml'//返回的类型为XML
                    success: function (result) {
                        try {
                            $(result).find("xmltest").each(function () {
                                alert($(this).find("Name").text() + " " + $(this).find("Age").text());
                            });
                        }
                        catch (e) {
                            alert(e);
                            return;
                        }
                    },
                    error: function (result, status) { //如果没有上面的捕获出错会执行这里的回调函数
                        if (status == 'error') {
                            alert(status);
                        }
                    }
                });
            });
        });

    </script>

    <div>
        <p>
            <input type="button" id="btn1" value="不带参" />
        </p>
        <p>
            <input type="button" id="btn2" value="带参" />
            <input type="text" id="txt" /></p>
        <p>
            <input type="button" id="btn3" value="对象" />
        </p>
        <p>
            <input type="button" id="btn4" value="集合" />
        </p>
        <p>
            <input type="button" id="btn5" value="XML" />
        </p>

    </div> 

    后台WebService:

    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        public class User
        {
            public string Name
            {
                get;
                set;
            }
            public int Age
            {
                get;
                set;
            }
        }
        /// <summary>
        
    /// 返回字符串
        
    /// </summary>
        
    /// <returns></returns>
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        /// <summary>
        
    /// 带参数
        
    /// </summary>
        
    /// <param name="s"></param>
        
    /// <returns></returns>
        [WebMethod]
        public string ReturnString(string s)
        {
            return s;
        }
        /// <summary>
        
    /// 返回对象
        
    /// </summary>
        
    /// <returns></returns>
        [WebMethod]
        public User ReturnDomain()
        {
            return new User { Name = "王斌", Age = 11 };
        }
        /// <summary>
        
    /// 返回集合对象
        
    /// </summary>
        
    /// <returns></returns>
        [WebMethod]
        public List<User> ReturnList()
        {
            return new List<User> {
                                    new User{Name="王斌",Age=23},
                                    new User{Name="李攀",Age=11}
            };
        }
        /// <summary>
        
    /// 返回XML
        
    /// </summary>
        
    /// <returns></returns>
        [WebMethod]
        public System.Data.DataTable ReturnXML()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Age"typeof(int));
            DataRow dr = dt.NewRow();
            dr[0] = "wangbin";
            dr[1] = 11;
            dt.Rows.Add(dr);
            DataRow dr1 = dt.NewRow();
            dr1[0] = "lipan";
            dr1[1] = 22;
            dt.Rows.Add(dr1);
            dt.TableName = "xmltest";
            return dt;
        }

    } 


    转载:http://blog.csdn.net/thebesttome/article/details/6921407

  • 相关阅读:
    Win8 iis 环境搭建
    Windows phone 8 触发器使用小结
    Windows Phone 页面之间参数传递方法
    日期SQL 脚本
    net 内存泄露和内存溢出
    Emacs的一些事情(与Vi的争议及使用)
    matlab与示波器连接及电脑连接
    msp430学习笔记-TA
    28个Unix/Linux的命令行神器
    linux在线中文手册
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/2876933.html
Copyright © 2020-2023  润新知