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){
Utility.RegisterTypeForAjax(typeof(User));
}
Allowing us to asynchronously call the GetUser in client-side code with code such as:
<script language= "javascript ">
function getUser(userId){
User.GetUser(GetUser_callback);
}
function GetUser_callback(response){
if (response != null && response.value != null){
var user = response.value;
if (typeof(user) == "object "){
alert(user.FirstName + " " + user.LastName);
}
}
}
getUser(1);
</script>
The value returned in the response is actually an object which exposes the same properties as a server-side object (FirstName, LastName and UserId).
Custom Converters
As we’ve seen, the Ajax .Net wrapper is able to deal with many different .Net types. However, aside from a handful of .Net classes and the built-in types, the wrapper simply calls ToString() on anything it can’t properly return. To get around this, the Ajax .Net wrapper allows developers to create object converters which can be used to smoothly communicate complex objects between the server and the client.
This guide will be updated with additional information on custom converters shortly (sorry).
Miscellaneous
Registering functions in another class
In the above example, our server-side functions resided within the code behind of the executing page. However, there’s no reason why these functions can’t be in a separate class file. Remember, the way the wrapper works is to find all methods within the specified class that have the Ajax.AjaxMethod. The class in question is specified via the second script tag. Using Ajax.Utility.RegisterTypeForAjax we can specify any class we want. For example, it would be reasonable to keep our server-side functions in a separate class:
Public Class AjaxFunctions
<Ajax.AjaxMethod()> _
Public Function Validate(username As String, password As String) As Boolean
'do something
'Return something
End Function
End Class
We could have the Ajax wrapper create proxies for this class by specifying this class’s type instead of the pages:
'Vb.Net
Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Ajax.Utility.RegisterTypeForAjax(GetType(AjaxFunctions))
'...
End Sub
//C#
private void Page_Load(object sender, EventArgs e){
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxFunctions));
//...
}
Remember, the client-side proxy takes the name of <ClassName> . <ServerSideFunctionName> . Therefore, if our ServerSideAdd function was located in the fictional AjaxFunctions class above, our client-side call would be: AjaxFunctions.ServerSideAdd(1,2)
----摘自ajax.net 1.1 文档
[解决办法]
我帮你顶上去
[解决办法]
AJAX 在回调方法里 把后台返回的对象转成
dataset 再拿到前台去处理就方便多了
[解决办法]
....
eval(WebProjectInfoBackWrite.getreturnObject(id));
....
window.close();
针对javascript编程,例如输出 "returnValue.value.KeyId= '82838423 ';alert( 'hello ');var x=1234; "等等,这就够了,如果你不需要javascript再“跨平台”就不要搞那么多花花肠子。
[解决办法]
[Ajax.AjaxMethod]
public DataSet getreturnObject(string id)
{
DataSet ds = //IList转成DataSet
return ds
}
[解决办法]
关于RenderControl的例子可以参考:
http://www.google.com/search?hl=zh-CN&newwindow=1&rls=com.microsoft%3Aen-US&q=render+%22new+stringwriter%22+%22new+htmltextwriter%22&btnG=Google+%E6%90%9C%E7%B4%A2&lr=lang_zh-CN%7Clang_zh-TW
不论是要刷新document上任意一个ui对象,还是要更新document中的一组变量(其实这也是对象),或者追加/修改/删除一个函数定义,都是可以用输出一个javascript片断(字符串)就可以了。客户端仅需要有一个通用的回调函数几乎只有一句“eval(result)”就可以了(可以增加一些异常处理、清理loading提示的代码等)。
基本上,ajax仍然可以使是很高级的服务器端编程技术,只要写少数几个javascript函数作客户端处理,当你需要将某些postback改为ajax的时候,例如将一个dropdownlist局部刷新的时候(autopostback来调用服务器端并根据值刷新页面上多个控件的时候),基本上不需要写一点javascript代码。
[解决办法]
sp说得有点道理,但是使用eval欠妥
把需要更改的内容放在div里面
ajax返回一个结果后修改div.innerHTML吧
[解决办法]
但是序列化这一部份用AJAX的话是它内部封装好方法了的,没办法知道。
=========
可以用反汇编看,