<!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>
<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 { get; set; }
public string Sex { get; set; }
}
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 { get; set; }
public string Sex { get; set; }
}
调用ashx时不要添加contentType: "application/json",服务端接收不了