Pure Ajax example: Reading data

Source: Internet
Author: User

<! 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> Ajax-only example: Reading data </title>
</Head>
<Body>

<SCRIPT type = "text/JavaScript">

Function ajaxcall ()
{
VaR XMLHTTP;
Try {// Firefox, opera 8.0 +, Safari
XMLHTTP = new XMLHttpRequest (); // instantiate the XMLHTTPRequest object
}
Catch (E)
{

// Internet Explorer 5 +
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
}
Catch (E)
{

Try
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (E)
{
Alert ("the browser does not support Ajax! ");
Return false;
}
}
}
// Bind a data processing function.
XMLHTTP. onreadystatechange = function ()
{
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
Document. getelementbyid ('txtresresult'). innerhtml = Document. getelementbyid ('txtusername'). Value + XMLHTTP. responsetext;
} Else
{
Alert ('request error .');
// There was a problem with the request,
// For example the response may be a 404 (not found)
// Or 500 (internal server error) Response codes
}

}
}
XMLHTTP. Open ("get", "data.txt", true); // asynchronous request data
XMLHTTP. Send (null );
}
</SCRIPT>

<Form style = "text-align: Left">
Name: <input type = "text" id = "txtusername" style = "width: 400px;"/>
<Br/>
Test: <input type = "button" id = "BTN" value = "test" onclick = "ajaxcall ();" style = "width: 400px;"/>
<Br/>
Result: <textarea id = "txtresult" style = "width: 400px;"> </textarea>

</Form>
</Body>
</Html>

Related Article

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.