AJAX的使用方法詳解
AJAX作為異步傳輸,局部刷新非常方便,用處很廣!
首先,對于AJAX的使用有4步:
1.創(chuàng)建AJAX對象
var xmlHttp = new XMLHttpRequest();
2.建立連接 (‘提交方式',‘Url地址')
xmlHttp.open('get','./AJAX_XML.xml');
3.判斷ajax準備狀態(tài)及狀態(tài)碼
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState==4 && xmlHttp.status==200) {
}
}
4.發(fā)送請求
xmlHttp.send(null); //get方式參數(shù)為null,post方式,參數(shù)為提交的參數(shù)
以下以異步提交用戶名(輸入用戶名之后,異步提交后臺判斷,前臺立馬提示是否已注冊,不用提交時再判斷!)
GET方式提交
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()方法中沒有數(shù)據(jù),要寫null
}
}
</script>
用戶名: <input type="text" name="" id="username">
xx.php
<?php print_r($_GET); ?>
1、 IE不支持中文
2、 =、&與請求的字符串的關(guān)鍵字相混淆。
POST提交
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>
用戶名: <input type="text" name="" id="username">
xx.php
<?php print_r($_POST); print_r($_GET); ?>
1、通過send()發(fā)送數(shù)據(jù)
2、必須設(shè)置setRequestHeader()將傳遞的參數(shù)轉(zhuǎn)成XML格式
3、post提交可以直接提交中文,不需要轉(zhuǎn)碼
4、post請求中的字符也會和URL中的&、=字符相混淆,所以建議也要使用encodeURIComponent()編碼
5、在POST提交的同時,可以進行GET提交
解決 IE不支持中文 =、&與請求的字符串的關(guān)鍵字相混淆 問題
在js中通過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()方法中沒有數(shù)據(jù),要寫null
}
}
1、req.responseText:獲取返回的字符串
2、req.responseXML:按DOM結(jié)構(gòu)獲取返回的數(shù)據(jù)
注意post/get兩種提交方式的區(qū)別!
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
PHP實現(xiàn)的隨機IP函數(shù)【國內(nèi)IP段】
這篇文章主要介紹了PHP實現(xiàn)的隨機IP函數(shù),可實現(xiàn)輸出國內(nèi)IP段的功能,涉及php字符串與數(shù)組的計算操作相關(guān)技巧,需要的朋友可以參考下2016-07-07
thinkphp Tp6經(jīng)常報錯 Call to undefined
在使用Tp6框架時經(jīng)常遇到"Call to undefined method"的錯誤,這里就為大家分享一下具體的使用方法,需要的朋友可以參考下2023-08-08
php-fpm超時時間設(shè)置request_terminate_timeout資源問題分析
之前發(fā)現(xiàn)一個php配置之后關(guān)于返回500和502的問題,今天看到一個兄弟寫的非常不錯,記錄一下2019-09-09
php獲取網(wǎng)卡的MAC地址支持WIN/LINUX系統(tǒng)
這篇文章主要介紹了使用php獲取網(wǎng)卡的MAC地址支持WIN/LINUX系統(tǒng),需要的朋友可以參考下2014-04-04
PHP和Java 集成開發(fā)詳解分析 強強聯(lián)合
很久以前,有人從www上看到看到天空上一個很亮的亮點,它就是Java語言,與此同時,在另一個地方一位夢想家也看到了一個亮點,它就是PHP.2008-11-11
php數(shù)組合并array_merge()函數(shù)使用注意事項
array_merge()函數(shù)在php中是對數(shù)組進行合并的,可以把多個數(shù)組合成一個數(shù)組,并且不改變原數(shù)組(www.111cn.net)的值了,但今天我在使用array_merge合并數(shù)組時碰到幾個小細節(jié)上的問題,下面我舉例子給各位朋友看看2014-06-06
PHP實現(xiàn)根據(jù)瀏覽器跳轉(zhuǎn)不同語言頁面代碼
以下是對使用PHP實現(xiàn)根據(jù)瀏覽器跳轉(zhuǎn)不同語言頁面的代碼進行了介紹,需要的朋友可以過來參考下2013-08-08

