日期:2014-05-20  浏览次数:20529 次

vs.net2003Ajax对象序列化问题
我用AJAX在客房端传一个ID到服务端查出一个对象然后返回到客户端,由于用的是面向对象的开发方法,所以返回来的也是一个对象,在查询一个简单对象(没有关联对象和ILIST集合)时能正确返回,当我那个对象里面组合了多个别的对象,还有ILIST集合时返回来就是空,在调试时在服务端代码里面己经正确查询出来的,是不是因为对象序列化后太大了的问题呢?AJAX有没有这种限制?我用的就是一个AJAX.DLL,
客房端调用函数:
function   R_Value(id)
{
alert(id)
try
{returnValue     =   WebProjectInfoBackWrite.getreturnObject(id);//WebProjectInfoBackWrite类中的getreturnObject查询出对象
alert(returnValue.value.KeyId)//查询简单对象时这里能正确读出对象的KeyId属性
window.close();
}
catch(err)
{
alert(err.description);
}
}

服务端方法:
[Ajax.AjaxMethod]
public   object   getreturnObject(string   id)
{

ProjectInfo   projectInfo   =   (ProjectInfo)doSelectDictionaryObject(typeof(ProjectInfo),id,true).ResultObject;
//查出来的类型是ProjectInfo   ,工程信息,里面组合了别的对象
string   ddd   =   projectInfo.KeyId;己经保证查出来的了

return   projectInf
}

------解决方案--------------------
可以,
Dealing with Types
Returning complex types
The Ajax wrapper is capable of handling more than just the integer our ServerSideAdd function returned. It currently supports integers, strings, double, booleans, DateTime, DataSets and DataTables, as well as the primitive types of custom classes and arrays. All other types have their ToString values returned.

Returned DataSets work much like real .Net DataSet. Given a server side function which returns a DataSet, we could display the contents client side via:

<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[i].FirstName + " </td> ";
s[s.length] = " <td> " + ds.Tables[0].Rows[i].Birthday + " </td> ";
s[s.length] = " </tr> ";
}
s[s.length] = " </table> ";
tableDisplay.innerHTML = s.join( " ");
}
else{
alert( "Error. [3001] " + response.request.responseText);
}
}
</script>


Ajax can also return custom classes. All that is required is that the class be marked with the Serializable attribute. Given the following class:

[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 ");
}
}

We would register the GetUser proxy via a call to the RegisterTypeForAjax:

private void Page_Load(object sender, EventArgs e){