javascript前端和后臺進(jìn)行數(shù)據(jù)交互方法示例
在開發(fā)中遇到了在沒有jQuery的情況下需要與后臺進(jìn)行部分?jǐn)?shù)據(jù)的交互,查找了部分資料使用JavaScript實(shí)現(xiàn)了操作,記錄一下。
//獲取XMLHttpRequest對象用于與后臺交互數(shù)據(jù)
function getXHR(){ var xmlHttp; try { xmlHttp=new XMLHttpRequest();//新版本瀏覽器 }catch(e) { try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//老版本瀏覽器 } catch(e) { alert("你的瀏覽器不支持ajax"); return false; } } } return xmlHttp; }
function check() { var pass = document.getElementById("pass").value; //1/得到xhr對象 var xhr=getXHR(); //2.注冊狀態(tài)變化監(jiān)聽器 xhr.onreadystatechange=function(){ if(xhr.readyState==4) { if(xhr.status==200) { var obj = document.getElementById("checkPass"); if("true" == xhr.responseText){ obj.innerHTML = "驗(yàn)證通過"; obj.style.color = "green"; }else{ obj.innerHTML = "原密碼輸入錯誤!"; obj.style.color = "brown"; return; } } } } //3.建立與服務(wù)器的連接(post請求方式,也可以使用get請求方式) xhr.open("post","請求數(shù)據(jù)的地址"); xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded'); //4.向服務(wù)器發(fā)出請求(使用post請求方式將數(shù)據(jù)發(fā)往后臺) xhr.send("pass="+pass); }
到此這篇關(guān)于javascript前端和后臺進(jìn)行數(shù)據(jù)交互方法的文章就介紹到這了,更多相關(guān)javascript前端和后臺數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用javascript實(shí)現(xiàn)鼠標(biāo)框選
用javascript實(shí)現(xiàn)鼠標(biāo)框選...2007-05-05javascript獲取鼠標(biāo)點(diǎn)擊元素對象(示例代碼)
本篇文章主要介紹了利用javascript獲取鼠標(biāo)點(diǎn)擊元素對象的示例代碼。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12JavaScript正則表達(dá)式匹配 div style標(biāo)簽
這篇文章主要介紹了JavaScript正則表達(dá)式匹配<div><style>標(biāo)簽 的相關(guān)資料,需要的朋友可以參考下2016-03-03

把input初始值不寫value的具體實(shí)現(xiàn)方法