Details about how to use AJAX and how to use AJAX
As asynchronous transmission, AJAX is very convenient and useful for local refreshing!
First, there are four steps to use AJAX:
1. Create an AJAX object
var xmlHttp = new XMLHttpRequest();
2. Establish a connection ('submission method ', 'url address ')
xmlHttp.open('get','./AJAX_XML.xml');
3. Determine the ajax preparation status and status code
xmlHttp.onreadystatechange = function(){ if (xmlHttp.readyState==4 && xmlHttp.status==200) { }}
4. Send a request
XmlHttp. send (null); // The get method parameter is null, THE post method, and the parameter is the submitted parameter.
The user name is submitted asynchronously. (After the user name is entered, the user name is submitted asynchronously to the background for determination. The foreground immediately prompts whether the user name has been registered and does not need to be submitted !)
Submit in GET Mode
Xx.html
<Script type = "text/javascript"> window. onload = function () {document. getElementById ('username '). onblur = function () {var name = document. getElementById ('username '). value; var req = new XMLHttpRequest (); req. open ('get', '4-demo. php? Name = '+ name); req. onreadystatechange = function () {if (req. readyState = 4 & req. status = 200) {alert (req. responseText) ;}} req. send (null); // If the send () method does not contain data, write null }}</script>
User name:<input type="text" name="" id="username">
Xx. php
<?phpprint_r($_GET);?>
1. IE does not support Chinese Characters
2. =, & is confused with the keyword of the request string.
POST submission
Xx.html
<script type="text/javascript">window.onload=function(){ document.getElementById('username').onblur=function(){ var name=document.getElementById('username').value; name=encodeURIComponent(name); var req=new XMLHttpRequest(); req.open('post','5-demo.php?age='+20); req.onreadystatechange=function(){ if(req.readyState==4 && req.status==200){ alert(req.responseText); } } req.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); req.send('name='+name); }}</script>
User name:<input type="text" name="" id="username">
Xx. php
<?phpprint_r($_POST);print_r($_GET);?>
1. send data through send ()
2. You must set setRequestHeader () to convert the passed parameters to XML format.
3. post submission can directly submit Chinese characters without Transcoding
4. The characters in the post request will also be confused with the &, = characters in the URL. Therefore, we recommend that you use encodeURIComponent () encoding.
5. GET submission can be performed at the same time as POST submission.
SolutionIE does not support Chinese =, & obfuscation with the keyword of the request stringProblem
In js, use encodeURIComponent () to encode it.
Window. onload = function () {document. getElementById ('username '). onblur = function () {var name = document. getElementById ('username '). value; name = encodeURIComponent (name); // encode var req = new XMLHttpRequest (); req. open ('get', '4-demo. php? Name = '+ name); req. onreadystatechange = function () {if (req. readyState = 4 & req. status = 200) {alert (req. responseText) ;}} req. send (null); // If the send () method does not contain data, write null }}
1. req. responseText: gets the returned string.
2. req. responseXML: Get the returned data according to the DOM Structure
Note the difference between the post and get submission methods!
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!