程序员人生 网站导航

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

栏目:jquery时间:2013-12-25 18:33:47

一、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>

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐