李空

博客园 首页 新随笔 联系 订阅 管理

前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-3.2-vsdoc2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" >
        jQuery(function () {
            jQuery.ajax({
                type: "Post",
                url: "/WebService1.asmx/HelloWorld",
                data:"{name:'likong'}",
                dataType: "json",
                contentType:"application/json;charset=utf-8",
                success: function (result) {
                    window.alert(result.d);
                },
                error:function(err){
                    window.alert(err.status);
                    
                }

            })

        })
    </script>
</head>
</html>
View Code

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    /// <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
    {

        [WebMethod]
        public string HelloWorld(string name)
        {
            return "na:" + name + "";
        }
    }
}
View Code

 Ajax调用WebService返回List数组:

前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-3.2-vsdoc2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" >
        jQuery(function () {
            jQuery.ajax({
                type: "Post",
                url: "/WebService1.asmx/HelloWorld",
                data:"{name:'likong',sex:'男',age:'22'}",
                dataType: "json",
               contentType:"application/json;charset=utf-8",
                success: function (result) {
                    var str;
                    jQuery.each(result.d, function (index,values) {
                        window.alert("" + (index+1) + "项;值为" + values + "");

                    })
                    
                },
                error:function(err){
                    window.alert(err.status);
                    
                }

            })

        })
    </script>
</head>
</html>
View Code

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    /// <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
    {

        [WebMethod]
        public List<string> HelloWorld(string name,string sex,string age)
        {
            List<string> list = new List<string>();
            list.Add(name);
            list.Add(sex);
            list.Add(age);
            return list;

        }
    }
}
View Code

Ajax调用WebService返回自定义类:

前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-3.2-vsdoc2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" >
        jQuery(function () {
            jQuery.ajax({
                type: "Post",
                url: "/WebService1.asmx/HelloWorld",
                data:"{name:'likong',sex:'男',age:'22'}",
                dataType: "json",
               contentType:"application/json;charset=utf-8",
                success: function (result) {
                    
                    window.alert(result.d.name + "          " + result.d.sex + "           " + result.d.age);
                    
                },
                error:function(err){
                    window.alert(err.status);
                    
                }

            })

        })
    </script>
</head>
</html>
View Code

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    /// <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
    {

        [WebMethod]
        public PerSon HelloWorld(string name,string sex,string age)
        {
            PerSon per = new PerSon();
            per.name = name;
            per.sex = sex;
            per.age = age;
            return per;

        }
    }

    public class PerSon
    {
        public string name {get;set; }
        public string sex { get; set; }
        public string age { get; set; }
    }
}
View Code

 Ajax调用WebService返回自定义类集合:

前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-3.2-vsdoc2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" >
        jQuery(function () {
            jQuery.ajax({
                type: "Post",
                url: "/WebService1.asmx/HelloWorld",
                data:"{name:'likong',sex:'男',age:'22'}",
                dataType: "json",
               contentType:"application/json;charset=utf-8",
                success: function (result) {
                    jQuery.each(result.d, function (index, values) {

                        window.alert("" + (index + 1) + "项:" + values.name + "          " + values.sex + "           " + values.age);

                    })
                    
                },
                error:function(err){
                    window.alert(err.status);
                    
                }

            })

        })
    </script>
</head>
</html>
View Code

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    /// <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
    {

        [WebMethod]
        public List<PerSon> HelloWorld(string name, string sex, string age)
        {
            return new List<PerSon>() { 
            new PerSon(){
                name=name,
                sex=sex,
                age=age
            },
            new PerSon(){
                name="张三",
                sex="",
                age="12"
            }
            
            };
        }
    }

    public class PerSon
    {
        public string name {get;set; }
        public string sex { get; set; }
        public string age { get; set; }
    }
}
View Code

  Ajax调用WebService返回Dictionary<string,string>集合:

前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-3.2-vsdoc2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" >
        jQuery(function () {
            jQuery.ajax({
                type: "Post",
                url: "/WebService1.asmx/HelloWorld",
                data:"{name:'likong',sex:'男',age:'22'}",
                dataType: "json",
               contentType:"application/json;charset=utf-8",
                success: function (result) {
                    jQuery.each(result.d, function (key, values) {

                        window.alert("" + key+ "" + values);

                    })
                    
                },
                error:function(err){
                    window.alert(err.status);
                    
                }

            })

        })
    </script>
</head>
</html>
View Code

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    /// <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
    {

        [WebMethod]
        public Dictionary<string,string> HelloWorld(string name, string sex, string age)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("1",name);
            dic.Add("2",sex);
            dic.Add("3", age);

            return dic;
        }
    }
}
View Code

Ajax调用WebService返回dataset:

前台代码:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-3.2-vsdoc2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" >
        jQuery(function () {
            jQuery.ajax({
                type: "post",
                url: "/WebService1.asmx/HelloWorld",
               data:"name=likong&sex=男&age=22",
                dataType: "xml",
                success: function (result) {
                    jQuery.each(jQuery.find("Table1", result), function () {

                        window.alert(jQuery(this).find("name").text() + "         " + jQuery(this).find("sex").text() + "           " + jQuery(this).find("age").text());

                    })
                    
                },
                error: function (data) {
                    //200的响应也有可能被认定为error,responseText中没有Message部分
                    alert($.parseJSON(data.responseText).Message);
                }


            })

        })
    </script>
</head>
</html>
View Code

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Data;

namespace WebApplication1
{
    /// <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
    {

        [WebMethod]
        public DataSet  HelloWorld(string name,string sex,string age)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("sex");
            dt.Columns.Add("age");
            dt.Rows.Add(name, sex, age);
            dt.Rows.Add("张三","","32");
            ds.Tables.Add(dt);

            return ds;
        }
    }
}
View Code

 

posted on 2013-12-12 22:24  李空  阅读(1689)  评论(0编辑  收藏  举报