我是 jquery 的新手。实际上我无法调用 ProcesAPI.aspx 页面中的 WebMethod。
[WebMethod(EnableSession = true)]
public string GetJsonData()
{
JavaScriptSerializer js = new JavaScriptSerializer();
var persons = new List<Person>
{
new Person{Id = 1, FirstName = "F1",
LastName = "L1",
Addresses = new List<Address>
{
new Address{Line1 = "LaneA"},
new Address{Line1 = "LaneB"}
}},
new Person{Id = 2, FirstName = "F2",
LastName = "L2",
Addresses = new List<Address>
{
new Address{Line1 = "LaneC"},
new Address{Line1 = "LaneD"}
}}};
string result = js.Serialize(persons);
return result;
}
我从中生成页面的页面是“MyForm.aspx”,这是我的页面 frpm,我正在调用我的 processApi GetJsonData 方法。
$(document).ready(function () {
$('#btnSubmit').click(function () {
$.getJSON('ProcessAPI.aspx\GetJsonData', null, function (data) {
var div = $('#ajaxDiv');
div.html("<br/> " + "Persons received from server: " + "<br/>");
});
alert('Hii I ');
return false;
});
});
function printPerson(div, item) {
div.append("<br/>" + "FName: " + item.FirstName + ", LName: " + item.LastName);
$.each(item.Addresses, function (i, addr) {
printAddress(div, addr);
});
}
function printAddress(div, item) {
div.append("<br/>" + " " + "Line1: " + item.Line1);
}
但我的方法没有被调用...... :(