Êëàññ îáíîâëåí, ïîñëåäíÿÿ âåðñèÿ 1.5. (
òûö ñþäà )
Êàê è îáùàë, äåëþñü àÿêñîâûì êëàññîì ñ ïóáëèêîé
Êëàññ óìååò:-
Ãîòîâèòü è ñòèðàòü - Âûïîëíÿòü çàïðîñû GET è POST (óäèâèòåëüíî!)
- Ôèëüòðóåò ïåðåäàâàåìûå â çàïðîñ ñïåöèàëüíûå ñèìâîëû (âñÿêèå &, =, ?)
- (ñëàäåíüêîå) Ïî æåëàíèþ âûïîëíÿåò JS êîä èç ðåçóëüòàòà çàïðîñà
- Åñòü âîçìîæíîñòü âûñòàâëÿòü ñâîè äåéñòâèÿ íà êàæäûé ñëó÷àé (ïîêà çàïðîñ îáðàáàòûâàåòñÿ, êîãäà ïðîèçîøëà îøèáêà, êîãäà ïðèøåë îòâåò)
Êîä êëàññà (òàêæå ïðèêðåïëåí)
PHP |
/** * AjaxRequest Class * Ðàçðàáîò÷èê: KiriK [mail@kirik.ws] * Äîêóìåíòàöèÿ: http://www.kirik.ws/ajax.html * * Ðàñïðîñòðàíÿåòñÿ ïîä ëèöåíçèåé GNU General Public License, version 3: * http://www.gnu.org/licenses/lgpl.txt **/ function AjaxRequest(url, execute) { var HttpRequest; var _OnLoad = function () { } var _OnComplete = function () { } var _OnError = function () { /* alert('AJAX query error.'); */ } var Execute = function (responseText) { var obj = document.createElement('div'); obj.innerHTML = responseText; var elts = obj.getElementsByTagName('script');
for(var i = 0; i < elts.length; i++) { eval(elts[i].text); elts[i].parentNode.removeChild(elts[i]); } }
this.OnLoad = function (fn) { _OnLoad = fn; }
this.OnComplete = function (fn) { _OnComplete = fn; }
this.OnError = function (fn) { _OnComplete = fn; }
this.Post = function (params) { if(this.RequestPrepare()) { params = this.StrPrepare(params); HttpRequest.open('POST', url, true); HttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8'); HttpRequest.setRequestHeader('Content-length', params.length); HttpRequest.setRequestHeader('Connection', 'close'); HttpRequest.send(params); } }
this.Get = function (params) { if(this.RequestPrepare()) { url += this.StrPrepare(params); HttpRequest.open('GET', url, true); HttpRequest.send(null); } }
this.RequestPrepare = function () { if (window.XMLHttpRequest) { /* Mozilla, Safari,... */ HttpRequest = new XMLHttpRequest(); if (HttpRequest.overrideMimeType) { HttpRequest.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { /* IE */ try { HttpRequest = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { HttpRequest = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { /* do nothing */ } } }
if (typeof HttpRequest == 'undefined') { alert('Cannot create XMLHTTP instance.'); return false; }
HttpRequest.onreadystatechange = function(e) { if (HttpRequest.readyState == 1) { _OnLoad(); }
if (HttpRequest.readyState == 4) { if(HttpRequest.status == 200) { if(execute === true) { Execute(HttpRequest.responseText); } _OnComplete(HttpRequest.responseText); } else { _OnError(HttpRequest.status); } } } return true; }
this.StrPrepare = function (obj) { if(obj instanceof Object) { var i = 0; var arr = []; for (var key in obj) { arr[i++] = encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]); } return arr.join('&'); } else { return ''; } } } |
Êðàòêîå îïèñàíèå: Çàäà÷àÏî íàæàòèþ êíîïêè "Îòïðàâèòü"-ïåðåäàòü
ñêðèïòó index.php äàííûå èç ïîëåé
name è
text ìåòîäîì POST, ïðè çàãðóçêå îòîáðàçèòü â äèâå òåêñò "Ïðîèñõîäèò çàãðóçêà..." à ðåçóëüòàò âûâåñòè â
div ñ
id ðàâíûì
result.
Ðåøåíèå ê çàäà÷åHTML |
<form onsubmit="return SendData(this)"> <input type="text" name="name" /> <input type="text" name="text" /> <input type="submit" value="Îòïðàâèòü" /> </form> <div id="result"></div> |
PHP |
function SendData(form) { // ñîçäàåì íîâîå ïîäêëþ÷åíèå var AJAX = new AjaxRequest('index.php');
// îïðåäåëÿåì ôóíêöèþ íà âûïîëíåíèå çàïðîñà AJAX.OnLoad(myOnLoad);
// îïðåäåëÿåì ôóíêöèþ íà çàâåðøåíèå çàïðîñà AJAX.OnComplete(myOnComplete);
// îïðåäåëÿåì ïåðåäàâàåìûå ïåðåìåííûå var data = { 'name': form.name.value, 'text': form.text.value }
AJAX.Post(data); // âûïîëíÿåì çàïðîñ
return false; // ôîðìó íå ñàáìèòèì }
function myOnLoad() { ge('result').innerHTML = 'Çàãðóçêà...'; }
function myOnComplete(result) { ge('result').innerHTML = result; } |
PHP |
function SendData(form) { var AJAX = new AjaxRequest('index.php');
AJAX.OnLoad(function () { ge('result').innerHTML = 'Çàãðóçêà...'; });
AJAX.OnComplete(function (result) { ge('result').innerHTML = result; });
AJAX.Post({'name': form.name.value, 'text': form.text.value});
return false; } |
Ñîáñòâåííî âîò. Òàê æå è ñ GET çàïðîñîì, òîëüêî âìåñòî
AJAX.Post íóæíî áóäåò íàïèñàòü
AJAX.GetÒîíêîñòè:Åñëè èñïîëüçóåòñÿ GET çïðîñ, òî íóæíî óêàçûâàòü
url ñ âîïðîñèòåëüíûì çíàêîì íà êîíöå, èëè ñ àìïåðñàíäîì:
index.php? èëè
index.php?act=remove&Åñëè õîòèì ÷òîáû
ñêðèïòû èç ðåçóëüòàòà çàïðîñà âûïîëíèëèñü, ïðè èíèöèàëèçàöèè êëàññà
execute ñòàâèì â ïîëîæåíèå
true:
var AJAX = new AjaxRequest('index.php', true);Åñëè õîòèì îïðåäåëèòü ñîáûòèå OnError, òî äåëàåì ýòî òàêæå êàê è âñå îñòàëüíûå, ïðè ýòîì â ôóíêöèþ áóäåò ïåðåäàâàòüñÿ ñòàòóñ îøèáêè.
Åñëè çàìåòèëè îøèáêè, èëè åñòü âîïðîñû, ïèøèòå