实用JS小汇总--实现异步获取数据
1<script type="text/javascript" src="jquery.js"></script>
2<script type="text/javascript" language="javascript">
3 xmlhttp = false;
4 var requestURL = "Chatcontent.aspx";
5function getNames()
6{
7 var url = requestURL;
8 getHTTPRequestObject();
9 if(xmlhttp)
10 {
11 xmlhttp.open("POST", url, true);
12 xmlhttp.onreadystatechange = callback;
13 xmlhttp.send(null);
14 }
15}
16
17function callback(response)
18{
19 if(xmlhttp.readyState == 4)
20 {
21 if(xmlhttp.status ==200)
22 {
23 eval("var objResults = " + xmlhttp.responseText);
24
25
26 var displaytext = "";
27
28 for (var i=0; i < objResults.Results.shops.length; i++)
29 {
30 displaytext += objResults.Results.shops[i].Name + "<br />";
31 }
32 if(displaytext.length > 0)
33 {
34 var findDIV = document.getElementById("chatcontent");
35 findDIV.className = 'show';
36 findDIV.innerHTML = displaytext;
37 }
38
39 }
40 }
41}
42
43function getHTTPRequestObject()
44{
45 try
46 {
47 // 首先尝试以前的遗留对象
48 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
49 }
50 catch(e)
51 {
52 try
53 {
54 // IE对象
55 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
56 }
57 catch(E)
58 {
59 xmlhttp = false;
60 }
61 }
62 if(!xmlhttp && typeof XMLHttpRequest!= 'undefined')
63 {
64 //其它对象,如Mozilla
65 xmlhttp = new XMLHttpRequest();
66 }
67}
68function begin()
69{
70 setInterval("getNames()", 1000);
71}
72
73</script>
获取ChatContent.aspx页面Reponse出来的值2<script type="text/javascript" language="javascript">
3 xmlhttp = false;
4 var requestURL = "Chatcontent.aspx";
5function getNames()
6{
7 var url = requestURL;
8 getHTTPRequestObject();
9 if(xmlhttp)
10 {
11 xmlhttp.open("POST", url, true);
12 xmlhttp.onreadystatechange = callback;
13 xmlhttp.send(null);
14 }
15}
16
17function callback(response)
18{
19 if(xmlhttp.readyState == 4)
20 {
21 if(xmlhttp.status ==200)
22 {
23 eval("var objResults = " + xmlhttp.responseText);
24
25
26 var displaytext = "";
27
28 for (var i=0; i < objResults.Results.shops.length; i++)
29 {
30 displaytext += objResults.Results.shops[i].Name + "<br />";
31 }
32 if(displaytext.length > 0)
33 {
34 var findDIV = document.getElementById("chatcontent");
35 findDIV.className = 'show';
36 findDIV.innerHTML = displaytext;
37 }
38
39 }
40 }
41}
42
43function getHTTPRequestObject()
44{
45 try
46 {
47 // 首先尝试以前的遗留对象
48 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
49 }
50 catch(e)
51 {
52 try
53 {
54 // IE对象
55 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
56 }
57 catch(E)
58 {
59 xmlhttp = false;
60 }
61 }
62 if(!xmlhttp && typeof XMLHttpRequest!= 'undefined')
63 {
64 //其它对象,如Mozilla
65 xmlhttp = new XMLHttpRequest();
66 }
67}
68function begin()
69{
70 setInterval("getNames()", 1000);
71}
72
73</script>
1 DataTable dtReturn = new DataTable();
2 string currentusername = "test03";//SPContext.Current.Web.CurrentUser.Name;
3 SqlConnection conn = new SqlConnection("Data Source=.;DataBase=oaApplication;Integrated Security=true");
4 string DeptName = "";
5 string strwhere = "";
6 strwhere = "(Ispublic='YES'and timenow>='" + DateTime.Now.ToShortDateString() + "')or (Ispublic='NO' and (user_to='" + currentusername + "' or user_from='" + currentusername + "') and timenow>='" + DateTime.Now.ToShortDateString() + "') or(Ispublic='NO' and user_to='" + DeptName + "'and timenow>='" + DateTime.Now.ToShortDateString() + "') order by ID desc";
7 conn.Open();
8 SqlCommand cmd = new SqlCommand(@"select top 10 ID,user_from,user_to,chatcontent,timenow,Ispublic from dbo.ChatRoom where "+strwhere+"", conn);
9 SqlParameter param = new SqlParameter();
10 SqlDataAdapter adpt = new SqlDataAdapter(cmd);
11 adpt.Fill(dtReturn);
12 conn.Close();
13 //构建JSON.
14 //dtReturn = BLL_ChatRoom.GetTenRecored(strwhere).Tables[0];
15 StringBuilder sb = new StringBuilder();
16 sb.Append("{\"Results\": { \"shops\": [");
17 for (int i = 0; i < dtReturn.Rows.Count; i++)
18 {
19 sb.Append("{\"Name\":\"" + Convert.ToDateTime(dtReturn.Rows[i]["timenow"]) + " " + (string)dtReturn.Rows[i]["user_from"] + "对" + (string)dtReturn.Rows[i]["user_to"] + " 说道<br/> " + (string)dtReturn.Rows[i]["chatcontent"] + " " + "\"}");
20 if (i <= (dtReturn.Rows.Count - 2))
21 {
22 sb.Append(",");
23 }
24 }
25
26 sb.Append("]}}");
27 Response.Write(sb.ToString());
2 string currentusername = "test03";//SPContext.Current.Web.CurrentUser.Name;
3 SqlConnection conn = new SqlConnection("Data Source=.;DataBase=oaApplication;Integrated Security=true");
4 string DeptName = "";
5 string strwhere = "";
6 strwhere = "(Ispublic='YES'and timenow>='" + DateTime.Now.ToShortDateString() + "')or (Ispublic='NO' and (user_to='" + currentusername + "' or user_from='" + currentusername + "') and timenow>='" + DateTime.Now.ToShortDateString() + "') or(Ispublic='NO' and user_to='" + DeptName + "'and timenow>='" + DateTime.Now.ToShortDateString() + "') order by ID desc";
7 conn.Open();
8 SqlCommand cmd = new SqlCommand(@"select top 10 ID,user_from,user_to,chatcontent,timenow,Ispublic from dbo.ChatRoom where "+strwhere+"", conn);
9 SqlParameter param = new SqlParameter();
10 SqlDataAdapter adpt = new SqlDataAdapter(cmd);
11 adpt.Fill(dtReturn);
12 conn.Close();
13 //构建JSON.
14 //dtReturn = BLL_ChatRoom.GetTenRecored(strwhere).Tables[0];
15 StringBuilder sb = new StringBuilder();
16 sb.Append("{\"Results\": { \"shops\": [");
17 for (int i = 0; i < dtReturn.Rows.Count; i++)
18 {
19 sb.Append("{\"Name\":\"" + Convert.ToDateTime(dtReturn.Rows[i]["timenow"]) + " " + (string)dtReturn.Rows[i]["user_from"] + "对" + (string)dtReturn.Rows[i]["user_to"] + " 说道<br/> " + (string)dtReturn.Rows[i]["chatcontent"] + " " + "\"}");
20 if (i <= (dtReturn.Rows.Count - 2))
21 {
22 sb.Append(",");
23 }
24 }
25
26 sb.Append("]}}");
27 Response.Write(sb.ToString());