asp.net中使用.ajax传参,从后台获得list数据,如何显示
asp.net中使用.ajax传参,从后台获得list数据,如何显示在前台?
看到很多说转化为json数据之后再回传。这个后台代码该如何书写呢?
接触asp.net才两个月,.ajax这种以前只在java中使用过。不过,貌似,还是有所区别的。
求解答啊。倒腾两天了,没结果。愁。
求代码,求代码啊。
非常感谢各位。
代码运行平台: VS2010,.net 4以上
前台代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Master/student.master" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="user_test2" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"><script type="text/javascript" src="../js/jquery-1.7.js"></script><script type="text/javascript"> $(function () { $("#<%=ceshi.ClientID %>").click(function () { var bianliang = "3"; alert(“****”);//显示正常 $.ajax({ type: "post", url: "test2.aspx/getschoolajax", contentType: "application/json; charset=utf-8", dataType: "json", data: "citynum=" + bianliang, beforeSend: function (XMLHttpRequest) { $("#schools").text("正在查询..."); }, success: function Msg(data) { $("#schools").css("color", "red"); $("#schools").html("-----data------" + data); }, error: function () { alert("error");//得不到数据,一直提示error } }); });</script></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><div> <asp:Button ID="ceshi" runat="server" Text="Button" /> <div id="schools" class="bg1"> <!-- 此处显示学校列表 --> </div><div></asp:Content>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using PBLL;using PModel;using System.Data;public partial class user_test2 : System.Web.UI.Page{ userinfoManager userinfo = new userinfoManager(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected List<string> getschoolajax() { int citynumber = Convert.ToInt32(Request.QueryString["citynum"]); List<string> schoollist = userinfo.getschoolbycitynumberlist(citynumber); if (schoollist.Count != 0) { return schoollist; } else { return null; } }}
/// <summary> /// Serialize object to string of json format /// </summary> private string JsonSerializer<T>(T t) { var ser = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// Deserialize string of json format to object /// </summary> private T JsonDeserialize<T>(string jsonString) { var ser = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); var obj = (T)ser.ReadObject(ms); return obj; }
[解决办法]
它的名字空间为:System.Web.Script.Serialization
如果要使用它,还须添加
System.Web.Extensions库文件引用
参考实体类:Customer
public class Customer
{
public int Unid { get; set; }
public string CustomerName { get; set; }
}
类JavaScriptSerializer描述:为启用 AFAX 的应用程序提供序列化和反序列化功能。
(一) 序列化
方法:public string Serialize(Object obj),用于将对象转换为 JSON 字符串
public string ScriptSerialize(Customer customer)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(customer);
}
测试:
Customer cc = new Customer { Unid = 1, CustomerName = "John" };
string strJson = ScriptSerialize(cc);
Console.WriteLine(strJson);
(二)反序列化
public Customer ScriptDeserialize(string strJson)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<Customer>(strJson);
}
通过Deserialize<T>方法来实现。
测试:
Customer c1 = ScriptDeserialize(strJson);
Console.WriteLine(c1.Unid + " " + c1.CustomerName);
(三)方法泛型
public string ScriptSerialize<T>(T t)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(t);
}
public T ScriptDeserialize<T>(string strJson)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<T>(strJson);
}
测试:
Customer cc = new Customer { Unid = 1, CustomerName = "John" };
string strJson = ScriptSerialize<Customer>(cc);
Console.WriteLine(strJson);
Customer c1 = ScriptDeserialize<Customer>(strJson);
Console.WriteLine(c1.Unid + " " + c1.CustomerName);
[解决办法]