Raw Ajax submits data--get way and post mode via the Send () method of the XMLHttpRequest object

Source: Internet
Author: User
//get Way to submit:
Xmlhttp.open ("Get", "testservlet?name=" +username,true);
Xmlhttp.send (NULL);

Post-mode requested code
Xmlhttp.open ("POST", "Testservlet", true);
Post method requires you to set the HTTP request headers yourself
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Post method to send data
Xmlhttp.send ("name=" +username);

The method of the user's famous school examination
This method uses the XMLHttpRequest object for AJAX asynchronous data interaction
var xmlhttp;


function Verify () {
1. Get the value in the text box using the DOM
Document.getelementbuid ("UserName") is a way to get element nodes in the DOM, an element node corresponding to a label in an HTML page, if the <input>
. Value can get the value of an element node
var userName = document.getElementById ("UserName"). Value;


//2. Create XMLHttpRequest Object
This is one of the most complicated steps in XMLHttpRequest's cash usage.
Need to write different code for IE and other types of browsers to create this object in an irregular way
if (window. XMLHttpRequest) {
For FIREFOX,MOZILLAR,OPERA,SAFARI,IE7,IE8
XMLHTTP = new XMLHttpRequest ();
Bug fixes for specific versions of Mozillar browsers
if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype ("Text/xml")
}
}else if (window. ActiveXObject) {
For Ie5,ie5.5,ie6 (IE7,IE8)
Two control names that can be used to create the XMLHttpRequest object, stored in a JS array
The previous version is newer
var activexname =["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
for (Var i=0;i<activexname.length;i++) {
try{
Takes out a control name to create, and terminates the loop if the creation succeeds
If the creation fails, throws an exception, and then continues the loop, continuing to attempt to create
XMLHTTP = new ActiveXObject (Activexname[i]);
Break
catch (e) {
}
}
}
Confirm that the XMLHttpRequest object was created successfully
if (!xmlhttp) {
Alert ("XMLHttpRequest object creation failed.) ");
Return
}else{
alert (XMLHTTP);
alert (xmlhttp.readystate);
//        }

2 Registering the callback function
To register a callback function, you need only the function name without parentheses
We need to register the function name, if the parentheses, it is the return value of the function to register, which is wrong
Xmlhttp.onreadystatechange = callback;

3 Setting up connection information
The first parameter represents the way HTTP is requested. Supports all HTTP request methods. Mainly using GET and post
The second parameter represents the URL address of the request, and the parameter to the Get method is also in the URL
The third parameter represents an asynchronous or synchronous interaction, and True indicates an asynchronous
Code requested by Get method
Xmlhttp.open ("Get", "testservlet?name=" +username,true);


4 sending data to start interacting with the server side
In sync, send is done after the server is set back
Asynchronously, send this sentence to the execution party immediately completed

Get mode
Xmlhttp.send (NULL);
}

//Callback function
function callback () {
//alert (xmlhttp.readystate);
//5 Receive response Data
//Determine the state of the object is interactive complete
if (xmlhttp.readystate==4) {
//To determine if HTTP interaction succeeded,
if ( xmlhttp.status==200) {
//Get server-side returned data
//Get plain Text data for server-side output
var responsetext = xmlhttp.responsetext;
// Display the data on the page
//Find the element node corresponding to the div tag by dom
var divnode = document.getElementById ("result");
//Set HTML content in element node
Divnode.innerhtml=responsetext;
}else
{
Alert ("error");
}
}

 
 

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.