JavaScript中的ajax功能的概念和示例詳解
AJAX即“Asynchronous Javascript And XML”(異步JavaScript和XML)。
個人理解:ajax就是無刷新提交,然后得到返回內(nèi)容。
對應(yīng)的不使用ajax時的傳統(tǒng)網(wǎng)頁如果需要更新內(nèi)容(或用php做處理時),必須重載整個網(wǎng)頁頁面。
示例:
html代碼如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax示例</title>
<style>
#loginForm {
border-collapse: collapse;
}
#loginForm,#loginForm td {
border:#550 1px solid;
text-align:center;
}
</style>
</head>
<body>
<table id="loginForm">
<tr>
<td>用戶名:</td>
<td><input type="text" id="userName"/></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" id="password"/></td>
</tr>
<tr>
<td colspan=2><input id="submitBtn" type="submit" value="ajax提交"/></td>
</tr>
</table>
<script type="text/javascript" language="javascript">
document.getElementById('submitBtn').addEventListener('click', clickSubmit);
function clickSubmit() {
makeRequest('./test.php');
}
var req = false;
/**
* 創(chuàng)建ajax請求
* url 處理請求的php位置
*/
function makeRequest(url) {
req = false;
//創(chuàng)建一個XMLHTPP實(shí)例
if (window.XMLHttpRequest) { // ie9以上和w3c瀏覽器的對象
req = new XMLHttpRequest();
if (req.overrideMimeType) {
//如果服務(wù)器的響應(yīng)沒有XML mime-type header,某些Mozilla瀏覽器可能無法正常工作.
//為了解決這個問題, 如果服務(wù)器響應(yīng)的header不是text/xml,可以調(diào)用其它方法修改該header.
req.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE678專用
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert('new window.ActiveXObject() failed!');
}
}
if (!req) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
//指定處理響應(yīng)的JavaScript函數(shù)名.
req.onreadystatechange = alertContents;
//測試傳遞 用戶名和密碼
var user_name = document.getElementById('userName').value;
var user_pwd = document.getElementById('password').value;
//open(請求方式,準(zhǔn)確的本域域名,是否異步);
//GET或POST方式
//----GET方式請求------
//req.open('GET', url+'?user_name='+user_name+'&user_pwd='+user_pwd, true);
//req.send(null);
//----POST方式請求------
//發(fā)送請求 如果open是POST方式可以發(fā)送字符串給服務(wù)器,也可以在open的第二個參數(shù)同時加上get傳輸
////req.open('POST', url+'?get1='+user_name+'&get2='+user_pwd, true);
req.open('POST', url, true);
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send('user_name='+user_name+'&user_pwd='+user_pwd);
}
/**
* ajax請求的回調(diào)處理函數(shù)
*/
function alertContents() {
if (req.readyState == 4) {
console.log(req.status);
if (req.status == 200) {
alert(req.response);
} else {
alert('There was a problem with the request.');
}
}
}
</script>
</body>
</html>
./test.php代碼如下
<?php
header('content-type:text/html;charset=utf-8');
var_dump($_POST);//獲取到post傳遞的數(shù)據(jù)
var_dump($_GET);
以上所述是小編給大家介紹的JavaScript中的ajax功能的概念和示例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript offsetX與layerX區(qū)別
FF沒有offsetX屬性,有個layerX屬性,只要將事件源的位置設(shè)置成相對定位(position:relative)或絕對定位(position:absolute),兩者結(jié)果就相等,表示事件源相對于父元素的X坐標(biāo)。2010-03-03
JavaScript異步調(diào)用定時方法并停止該方法實(shí)現(xiàn)代碼
JavaScript異步調(diào)用定時方法并停止該方法實(shí)現(xiàn)代碼 ,需要的朋友可以參考下2012-03-03
Bootstrap Metronic完全響應(yīng)式管理模板學(xué)習(xí)筆記
這篇文章主要為大家分享了Bootstrap Metronic完全響應(yīng)式管理模板學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下2016-07-07

