惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
Webroot Blog
Webroot Blog
S
Security Affairs
H
Hacker News: Front Page
TaoSecurity Blog
TaoSecurity Blog
W
WeLiveSecurity
G
GRAHAM CLULEY
T
Tenable Blog
Schneier on Security
Schneier on Security
S
Securelist
Cyberwarzone
Cyberwarzone
P
Privacy International News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
D
DataBreaches.Net
博客园_首页
MyScale Blog
MyScale Blog
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
J
Java Code Geeks
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 大老鼠

JQuery ajax 传递数组 自定义属性 JQuery元素坐标,偏移量 IE8按F12不显示开发人员工具窗口 JQuery Validate验证表单元素 - 大老鼠 - 博客园 序列化/反序列化JSON sqlserver使用select加锁 jQuery操作解析JSON (sp_dbcmptlevel)将某些数据库行为设置为与指定的 SQL Server 版本兼容。 jQuery使用手册 效果-为操作添加艺术性 DOM操作-基于命令改变页面 事件 选择符-取得你想要的一切 jQuery入门 动态修改样式和层叠样式表 - 大老鼠 - 博客园 DOM2核心和DOM2 HTML 创建可重用的对象 JavaScript中常见陷阱 - 大老鼠 - 博客园
JQuery AJAX调用WEB SERVICE方法
大老鼠 · 2010-07-06 · via 博客园 - 大老鼠
类定义
    /// <summary>
    /// 员工信息
    /// </summary>
    public class EmployeeEntity{
        /// <summary>
        /// 员工姓名
        /// </summary>
        public string EmployeeName{get;set;}

        /// <summary>
        /// 生日
        /// </summary>
        public string Birthday{get;set;}
    }

WEB SERVICE定义

    /// <summary>
    /// JQuery测试服务
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService{
        /// <summary>
        /// 不含参数测试
        /// </summary>
        /// <returns>返回Hello World</returns>
        [WebMethod]
        public string HelloWorld(){
            return "Hello World";
        }

        /// <summary>
        /// 带参数测试
        /// </summary>
        /// <param name="yourName">你的姓名</param>
        /// <param name="age">年龄</param>
        /// <returns>返回一段问候</returns>
        [WebMethod]
        public string GetHello(string yourName, int age){
            return string.Format("Hello {0} , your age is {1}", yourName, age);
        }

        /// <summary>
        /// 获得类实例
        /// </summary>
        /// <returns>员工信息实体</returns>
        [WebMethod]
        public EmployeeEntity GetEmployeeEntity(){
            return new EmployeeEntity(){
                EmployeeName = "张三",
                Birthday = "2000-07-12"
            };
        }

        /// <summary>
        /// 获得集合对象
        /// </summary>
        /// <returns>集合对象</returns>
        [WebMethod]
        public List<string> GetList(){
            List<string> countryList = new List<string>();

            countryList.Add("中国");
            countryList.Add("美国");
            countryList.Add("法国");
            countryList.Add("德国");

            return countryList;
        }

        /// <summary>
        /// 获得DataSet
        /// </summary>
        /// <returns>DataSet对象</returns>
        [WebMethod]
        public DataSet GetDataSet(){
            DataSet     dataSet = new DataSet();
            DataTable   dataTable = new DataTable("employeeTable");

            dataTable.Columns.AddRange(new DataColumn[]{
                new DataColumn("EmployeeName",typeof(string)),
                new DataColumn("Age",typeof(int)),
                new DataColumn("Birthday",typeof(string))
            });

            dataTable.Rows.Add(new object[] { "李白", "63", "1320-05-06" });
            dataTable.Rows.Add(new object[] { "王刚", "60", "1920-05-06" });
            dataTable.Rows.Add(new object[] { "李毅", "53", "1820-05-06" });

            dataSet.Tables.Add(dataTable);
            return dataSet;
        }
    }
调用页面
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>JQuery AJAX调用各种WEB SERVICE</title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //不带参数调用
            $('#callWithNoParameter').click(function (event) {
                $.ajax({
                    type: 'POST',
                    contentType: 'application/json',
                    url: 'http://localhost:8117/Service1.asmx/HelloWorld',
                    data: '{}',
                    dataType: 'json',
                    success: function (result) {
                        alert(result.d);
                    }
                });
                event.preventDefault();
            });

            //带参数调用
            $('#callWithParameter').click(function (event) {
                $.ajax({
                    type: 'POST',
                    contentType: 'application/json',
                    url: 'http://localhost:8117/Service1.asmx/GetHello',
                    data: '{yourName:"李四",age:25}',
                    dataType: 'json',
                    success: function (result) {
                        alert(result.d);
                    }
                });

                event.preventDefault();
            });

            //返回类实例对象
            $('#getClassInstance').click(function (event) {
                $.ajax({
                    type: 'POST',
                    contentType: 'application/json',
                    url: 'http://localhost:8117/Service1.asmx/GetEmployeeEntity',
                    data: '{}',
                    dataType: 'json',
                    success: function (result) {
                        $(result.d).each(function () {
                            alert('你的姓名是:' + this['EmployeeName'] + ' 生日是:' + this['Birthday']);
                        });
                    }
                });

                event.preventDefault();
            });

            //返回集合
            $('#getList').click(function (event) {
                $.ajax({
                    type: 'POST',
                    contentType: 'application/json',
                    url: 'http://localhost:8117/Service1.asmx/GetList',
                    data: '{}',
                    dataType: 'json',
                    success: function (result) {
                        $(result.d).each(function () {
                            alert(this.toString());
                        });
                    }
                });

                event.preventDefault();
            });

            //返回DataSet
            $('#getDataSet').click(function (event) {
                $.ajax({
                    type: 'POST',
                    url: 'http://localhost:8117/Service1.asmx/GetDataSet',
                    data: '{}',
                    dataType: 'xml',
                    success: function (result) {
                        try {
                            $(result).find('employeeTable').each(function () {
                                alert(
                                    '姓名=' + $(this).find('EmployeeName').text() + ';' +
                                    '年龄=' + $(this).find('Age').text() + ';' +
                                    '生日=' + $(this).find('Birthday').text());
                            });
                        }
                        catch (e) {
                            alert(e);
                        }
                    },
                    error: function (result, status) {
                        alert(status);
                    }
                });

                event.preventDefault();
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <button id="callWithNoParameter">不带参数调用</button>
        <button id="callWithParameter">带参数调用</button>
        <button id="getClassInstance">获得类实例</button>
        <button id="getList">获得集合</button>
        <button id="getDataSet">获得DataSet</button>
    </div>
    </form>
</body>
</html>