软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > Web前端 > jQuery > Jquery Ajax实例4:向WebService发出请求 返回实体对象的异步调用

Jquery Ajax实例4:向WebService发出请求 返回实体对象的异步调用

一、WebService.asmx:

处理业务数据,在GetPerson方法中产生Person实体类数据,供JqueryRequest.aspx调用,代码如下:

[WebMethod]
public Person GetPerson(string name, int age, string address)
{
Person p = new Person()
{
Name = name,
Age = age,
Address = address
};
return p;
}

二、Person.cs实体类:

public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
private string _address;
public string Address
{
get { return _address; }
set { _address = value; }
}
}

三、AjaxRequest.aspx

通过点击按钮来请求WebService.asmx的GetPerson(string name, int age, string address)方法,获取Person实体数据。代码如下:

<script type="text/javascript" language="javascript">
function BtnPerson_Click() {
$.ajax({
url: "WebService.asmx/GetPerson",
data: { name: "Windy", age: 18, address: "Liehuo.Net Shanghai China" },
type: "post",
success: function(data, status) {
var obj = $(data);
var name = obj.find("Name").text();
var age = obj.find("Age").text();
var address = obj.find("Address").text();
alert("Name:" + name + " Age:" + age + " Address:" + address);
}
}); //liehuo.net
}
</script>

    相关阅读
    栏目导航
    推荐软件