Ajax Get Data code

Source: Internet
Author: User

Get string without flush:

In HTML pages:

<script>

Defining asynchronous objects
var xmlHttp;

Encapsulation method
function Createxmlhttp () {
try {
XmlHttp = new XMLHttpRequest ();
} catch (e) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}
Window.onload = function () {
document.getElementById ("name"). onclick = function () {

//Create asynchronous Object
Createxmlhttp ();

//through events to accept server return
Xmlhttp.onreadystatechange = function () {

  Judging status
if (xmlhttp.readystate==4&& xmlhttp.status==200) {

    Get the data you want
var data = Xmlhttp.responsetext;
document.getElementById ("D1"). InnerText = data;
}
}

//Set the address and manner of the server to be accessed
Xmlhttp.open ("Get", "data.ashx");

    Sending an asynchronous request
Xmlhttp.send ();
}
}
</script>
<body>
<div id= "D1" ></div>
<input type= "button" id= "name" value= "Get Data"/>
</body>

Provide data page: Data.ashx:

public void ProcessRequest (HttpContext context) {
Context. Response.ContentType = "Text/plain";

  Thread takes two seconds to get data
System.Threading.Thread.Sleep (2000);

//data provided by you--"string
Context. Response.Write ("The data you Want");
}

-----------------------------------------------------------------------------------------------------------

No refresh gets a table that deletes data without flushing in a static web page

In HTML pages:

<body>
<input type= "button" id= "name" value= "Get Table"/>
<div id= "D1" ></div>
</body>
<script>

var xmlHttp;
function Createhttp () {
try {
XmlHttp = new XMLHttpRequest ();
} catch (e) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}

Encapsulate the same Code
function Common () {
Createhttp ();
Xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) {

    Accept data--"a string of a table
var table = Xmlhttp.responsetext;

    Display in a Web page
document.getElementById ("D1"). InnerHTML = table;
}
}
}
document.getElementById ("name"). onclick = function () {
Common ();
Xmlhttp.open ("Get", "table.ashx");
Xmlhttp.send ();
}

Delete a linked script
function dd (ID) { //id the deleted ID passed over
Common ();
Xmlhttp.open ("Get", "table.ashx?id=" + ID);
Xmlhttp.send ()
}
</script>

Provide data page: Table.ashx:

public void ProcessRequest (HttpContext context) {
Context. Response.ContentType = "Text/plain";

If no value is represented to get a table
if (context. request.querystring["id"]!=null)
{

   Get the ID you want to delete
int id = Int. Parse (context. request.querystring["id"]. ToString ());
int r = sqlhelper.executenonquery ("delete Users where [email protected]", CommandType.Text, id);
if (r>0)
{
Common (context);
}
}
Else
{
Common (context);
}
}

//Get a table of strings

public void Common (HttpContext context)
{
SqlDataReader dr = Sqlhelper.executereader ("SELECT * from Users", CommandType.Text);
String newtable = "<table cellspacing= ' 0 ' rules= ' all ' border= ' 1 ' id= ' GridView1 ' style= ' border-collapse:collapse; ' ><tr><th scope= ' col ' >ID</th> ' +

"<th scope= ' col ' >username</th><th scope= ' col ' >password</th><th scope= ' col ' >Sex</ th><th scope= ' col ' >adminid</th><th scope= ' col ' > delete ' + ' </th></tr> ';

//Fill out a table by reading rows of data

while (Dr. Read ())
{
string sex = "checked= ' Checke '";
if (Dr[4]. ToString (). ToLower () = = "false")
{
sex = "";
}
NewTable + = "<tr><td>" + dr[0] + "</td><td>" + dr[1 "+" </td><td> "+ dr[2" + "</td& Gt;<td><span class= ' aspnetdisabled ' ><input type= ' checkbox ' "+ Sex +" disabled= ' disabled '/></ Span></td><td> "+ dr[3" + "</td><td>" +

//javascript:dd ("+dr[0]+") This is a script implemented in a static web page.
"<a href= ' javascript:dd (" + dr[0] + ") ' > Delete </a></td></tr> ';
}
Dr. Close ();
NewTable + = "</table>";
Context. Response.Write (newtable);
}

Ajax Get Data code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.