在云那方

首页 新随笔 联系 订阅 管理
 
<!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>
    <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(
function () {
            $(
"#noparams").click(function () {
                $.ajax({
                    type: 
"post",
                    url: 
"Default.aspx/GetHelooWorld",
                    contentType: 
"application/json",
                    dataType: 
"json",
                    data: 
"{}",
                    error: 
function () { alert("error") },
                    success: 
function (json) {
                        alert(json.d);
                    }
                });
            });

            $(
"#params").click(function () {
                $.ajax({
                    type: 
"post",
                    url: 
"Default.aspx/GetCount",
                    contentType: 
"application/json",
                    dataType: 
"json",
                    data: 
"{a:1,b:2}",
                    error: 
function () { alert("error") },
                    success: 
function (json) {
                        alert(json.d);
                    }
                });
            });

            $(
"#obj").click(function () {
                $.ajax({
                    type: 
"post",
                    url: 
"Default.aspx/GetUser",
                    contentType: 
"application/json",
                    dataType: 
"json",
                    data: 
"{}",
                    error: 
function () { alert("error") },
                    success: 
function (json) {
                        
var user = json.d;
                        alert(
"姓名:" + user.Name + ",性别:" + user.Sex);
                    }
                });
            });

            $(
"#objlist").click(function () {
                $.ajax({
                    type: 
"post",
                    url: 
"Default.aspx/GetUserList",
                    contentType: 
"application/json",
                    dataType: 
"json",
                    data: 
"{}",
                    error: 
function () { alert("error") },
                    success: 
function (json) {
                        
var list = json.d;
                        $(
"#table [myname=row]").remove();
                        $.each(list, 
function (index, user) {
                            $(
"#table").append("<tr myname='row'>\
                                                <td>
" + user.Name + "</td>\
                                                <td>
" + user.Sex + "</td>\
                                                </tr>
");
                        });
                    }
                });
            });
        });
    
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="noparams" type="button" value="不带参数的调用" />
        <input id="params" type="button" value="带参数的调用" />
        <input id="obj" type="button" value="返回对象的调用" />
        <input id="objlist" type="button" value="返回列表对象的调用" />
    </div>
    <table id="table">
        <tr>
            <td>
                姓名
            </td>
            <td>
                性别
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

 

 

 

[WebMethod]
public static string GetHelooWorld()
{
    return "Hello World!";
}

[WebMethod]
public static int GetCount(int a, int b)
{
    return a + b ;
}

[WebMethod]
public static User GetUser()
{
    return new User
    {
        Name = "李三",
        Sex = ""
    };
}

[WebMethod]
public static List<User> GetUserList()
{
    var list = new List<User>();
    list.Add(new User
    {
        Name = "李三",
        Sex = ""
    });

    list.Add(new User
    {
        Name = "李四",
        Sex = ""
    });

    return list;
}

public class User
{
    public string Name { getset; }
    public string Sex { getset; }
}

 调用ashx时不要添加contentType: "application/json",服务端接收不了

posted on 2012-07-04 00:36  Rich.T  阅读(210)  评论(0编辑  收藏  举报