〈script language="JavaScript">
//Asynchronous call to the mythical "GetDataSet" server-side function
function getDataSet(){
AjaxFunctions.GetDataSet(GetDataSet_callback);
}
function GetDataSet_callback(response){
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null){
var s = new Array();
s[s.length] = "〈table border=1>";
for(var i=0; i〈ds.Tables[0].Rows.length; i++){
s[s.length] = "〈tr>";
s[s.length] = "〈td>" + ds.Tables[0].Rows.FirstName + "〈/td>";
s[s.length] = "〈td>" + ds.Tables[0].Rows.Birthday + "〈/td>";
s[s.length] = "〈/tr>";
}
s[s.length] = "〈/table>";
tableDisplay.innerHTML = s.join("");
}
else {
alert("Error. [3001] " + response.request.responseText);
}
}
〈/script>
Ajax还可以返回自定义类,唯一的要求是必须用Serializable属性标记。假设有如下的类:
[Serializable()]
public class User{
private int _userId;
private string _firstName;
private string _lastName;
public int userId{
get { return _userId; }
}
public string FirstName{
get { return _firstName; }
}
public string LastName{
get { return _lastName; }
}
public User(int _userId, string _firstName, string _lastName){
this._userId = _userId;
this._firstName = _firstName;
this._lastName = _lastName;
}
public User(){}
[AjaxMethod()]
public static User GetUser(int userId){
//Replace this with a DB hit or something :)
return new User(userId,"Michael", "Schwarz");
}
}
Public Class AjaxFunctions
〈Ajax.AjaxMethod()> _
Public Function Validate(username As String, password As String) As Boolean
''do something
''Return something
End Function
End Class
[Ajax.AjaxMethod]
public string Test1(string name, string email, string comment){
string html = "";
html += "Hello " + name + "〈br>";
html += "Thank you for your comment 〈b>";
html += System.Web.HttpUtility.HtmlEncode(comment);
html += "〈/b>.";
return html;
}