积极答复者
使用jquery的ajax方法,从客户端post数据到服务器段。如何处理数据的序列化问题?

问题
-
客户端代码如下:
var v = { data: "abc", action: [{ name: "Hugo", Age: 23}] }; $.ajax({ type: "get", url: "Default2.aspx", data: v, datatype: "json", success: function(reasult) { alert(reasult); } })
在服务器端(这里的Default2.aspx)我要读json字符串里面的action字段内的数据,我应该怎么做呢?如果直接使用Request.Form["action"]得到的是个字符串:"[object Object]"。
郭鹏
答案
-
你好,
你可以到下面网站下载一个juery脚本,引用到你的项目中稍微做下处理即可。
http://code.google.com/p/jquery-json/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="jquery-1.3.2.js" type="text/javascript"></script> <script src="jquery.json-2.2.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#Text1").click(function() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "WebService.asmx/HelloWorld", data: "{}", dataType: "json", success: function(data) { displayResult(data); } }) } ); function displayResult(data) { alert(data.d); } var vobj = { _type: "Person", name: "Hugo", age: 23, address: { country: "china" } } $("#Button1").click(function() { $.ajax({ type: "POST", url: "Default.aspx", dataType: "json", data:$.toJSON(vobj) }) }); }); </script> </head> <body> <div> <input id="Text1" type="text" /></div> <input id="Button1" type="button" value="button" /> </body> </html>
得到的结果如下:Request.Form[0]="{"_type":"Person", "name":"Hugo", "age":23, "address":{"country":"china"}}“
Microsoft Online Community Support- 已标记为答案 Hugo12345 2009年9月23日 5:39
全部回复
-
你好,使用下面地址的方法即可:
知识改变命运,奋斗成就人生!- 已标记为答案 KeFang Chen 2009年9月17日 2:20
- 取消答案标记 Hugo12345 2009年9月18日 10:49
-
是jquery吧
$.ajax({
type: "post",
url: "Default2.aspx",
data: “ name=Hugo &Age= 23”,
datatype: "json",
success: function(reasult) {
alert(reasult);
}
服务端
用Request.Form["name"]或者Request.Form["Age"]
如果你想传对象方面的话 我建议用asp.net ajax+web服务可以- 已标记为答案 KeFang Chen 2009年9月17日 2:22
- 取消答案标记 Hugo12345 2009年9月18日 10:49
-
参考X.X.Y给的链接。如果我发往服务器端的ajax请求代码如下(jQuery):
$.ajax({ type: "post", url: "Default2.aspx", dataType: "json", data: { name: "Hugo", age: 23, address: { country:"china", city:"ChongQing" } } });
在服务器端,也就是这里的Default2.aspx页面,我进行这样的处理:
byte[] fByteString = new byte[Request.InputStream.Length]; Request.InputStream.Read(fByteString, 0, fByteString.Length); string str = System.Text.Encoding.Default.GetString(fByteString);
读取到的数据如下:name=Hugo&age=23&address=%5Bobject+Object%5D。
如果客户端发向服务器端的json字符串结构简单的话使用这样的方法是没问题的。但是如果内部还有其他的对象,这样的方法仍然没办法解析。我应该怎么才能正确的解析呢?
郭鹏 -
-
你好,
你可以到下面网站下载一个juery脚本,引用到你的项目中稍微做下处理即可。
http://code.google.com/p/jquery-json/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="jquery-1.3.2.js" type="text/javascript"></script> <script src="jquery.json-2.2.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#Text1").click(function() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "WebService.asmx/HelloWorld", data: "{}", dataType: "json", success: function(data) { displayResult(data); } }) } ); function displayResult(data) { alert(data.d); } var vobj = { _type: "Person", name: "Hugo", age: 23, address: { country: "china" } } $("#Button1").click(function() { $.ajax({ type: "POST", url: "Default.aspx", dataType: "json", data:$.toJSON(vobj) }) }); }); </script> </head> <body> <div> <input id="Text1" type="text" /></div> <input id="Button1" type="button" value="button" /> </body> </html>
得到的结果如下:Request.Form[0]="{"_type":"Person", "name":"Hugo", "age":23, "address":{"country":"china"}}“
Microsoft Online Community Support- 已标记为答案 Hugo12345 2009年9月23日 5:39