Home > Article > Backend Development > Detailed explanation of how to use ajax
AJAX is a technology for exchanging data with a server that updates part of a web page without reloading the entire page. This article mainly introduces the relevant knowledge about the use of AJAX.
AJAX is an asynchronous transmission, partial refresh is very convenient and has many uses!
First, there are 4 steps for using 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 ajax preparation status and status code
xmlHttp.onreadystatechange = function(){ if (xmlHttp.readyState==4 && xmlHttp.status==200) { } }
4. Send request
xmlHttp.send(null); //get method parameter is null, post method, the parameter is the submitted parameter
Submit by GET method
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); //如果send()方法中没有数据,要写null } } </script>Username:
421386b101eb8d5ec278372a19030bc4
##xx.php<?php
print_r($_GET);
?>
1、 IE does not support Chinese
2、 =, & are confused with the keywords of the requested
stringPOST 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>
Username :
xx.php <?php
print_r($_POST);
print_r($_GET);
?>
1. Send data through send()
2. SetRequestHeader() must be set to convert the passed parameters into XML format
3. Post submission can directly submit Chinese, no need Transcoding
4, characters in
post requestwill also be confused with the & and = characters in the URL, so it is recommended to also use encodeURIComponent() encoding5 , At the same time as POST submission, GET submission can be performed
Solution
IE does not support Chinese =, & is confused with the keywords of the requested string Problem Just encode in js through encodeURIComponent().
window.onload=function(){ document.getElementById('username').onblur=function(){ var name=document.getElementById('username').value; name=encodeURIComponent(name); //编码 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); //如果send()方法中没有数据,要写null } }
1. req.responseText: Get the returned string
2. req.responseXML: Get the returned data according to the DOM structure
The above is the detailed content of Detailed explanation of how to use ajax. For more information, please follow other related articles on the PHP Chinese website!