如何十分鐘學(xué)會 xajax_AJAX教程
推薦:AJAX快速入門之HTTP協(xié)議基礎(chǔ)要很好地領(lǐng)會Ajax技術(shù)的關(guān)鍵是了解超文本傳輸協(xié)議(HTTP),該協(xié)議用來傳輸網(wǎng)頁、圖像以及因特網(wǎng)上在瀏覽器與服務(wù)器間傳輸?shù)钠渌愋臀募�。只要你在瀏覽器上輸入一個URL,最前面的http://就表示使用HTTP來訪問指定位置的信息。(大部分瀏覽器還支持其他一些
譯者按: xajax 最大的特點是他采用了xml response,這樣我們可以用php來布置,處理異步傳送數(shù)據(jù)之后,網(wǎng)頁內(nèi)容的更新。而這些操作其它的ajax 框架都是由js來完成的的。xajax 使我們只需要寫一些php函數(shù),就可以實現(xiàn)。
所有學(xué)好xajax的關(guān)健在于熟練掌握 xajaxresponse 類。
tutorials:learn xajax in 10 minutes
教程:十分鐘學(xué)會 xajax
using xajax in a php script
一個使用的xajax的php腳本:
include the xajax class library:
調(diào)用xajax類庫:
require_once("xajax.inc.php");
instantiate the xajax object:
實例化xajax對象
xajax = new xajax();
register the names of the php functions you want to be able to call through xajax:
注冊一個你想用xajax來調(diào)用的php函數(shù)名(與javascript中的函數(shù)名相對應(yīng) xajax_myfunction)
xajax->registerfunction("myfunction");
write the php functions you have registered and use the xajaxresponse object to return xml commands from them:
編寫那個你剛剛已經(jīng)注冊的php函數(shù),并從中用 xajaxresponse 對象來返回xml指令集
function myfunction(arg)
{
// do some stuff based on arg like query data from a database and
// put it into a variable like newcontent
//對參數(shù)arg做一些諸如:從數(shù)據(jù)庫中獲取數(shù)據(jù)后定義給newcontent 變量的基本操作
// instantiate the xajaxresponse object
//實例化 xajaxresponse 對象
objresponse = new xajaxresponse();
// add a command to the response to assign the innerhtml attribute of
// the element with id="someelementid" to whatever the new content is
// 在響應(yīng)實例中添加一個命令,用來將id為someelementid的innerhtml元素屬性
// 變?yōu)槿魏涡碌膬?nèi)容.
objresponse->addassign("someelementid","innerhtml", newcontent);
//return the xml response generated by the xajaxresponse object
//返回由 xajaxresponse 對象所生成的xml 響應(yīng)
return objresponse->getxml();
}
before your script sends any output, have xajax handle any requests:
在你腳本傳送出任何東西前,xajax都要處理所有請求
xajax->processrequests();
between your <head></head> tags, tell xajax to generate the necessary javascript:
在該頁的<head>和</head>標簽之間插入下列代碼,使xajax實例可以自己生成所必需的js
<?php xajax->printjavascript(); ?>
call the function from a javascript event or function in your application:
從你程序中的js 事件或函數(shù)調(diào)用之前你已經(jīng)注冊過的相對應(yīng)函數(shù)
<div id="someelementid"></div>
<button onclick="xajax_myfunction(someargument);">
that's it. xajax takes care of most everything else. your biggest task is writing the php functions and returning xajax xml responses from them-- which is made extremely easy by the xajaxresponse class.
只需這些步驟。其他的交由xajax 去處理吧。你最主要的任務(wù)只是編寫php中的函數(shù),只要使它們能返回xajax的xml響應(yīng)就行了,而這步可以用xajaxresponse 類輕松解決。
how do i update my content asynchronously?
如何異步更新我的內(nèi)容?
perhaps the most unique feature of xajax is the xajaxresponse class. other ajax libraries require you to write your own callback handlers in javascript to process the data returned from an asynchronous request and to update the content. xajax, on the other hand, allows you to easily control your content from php. the xajaxresponse class allows you to create xml instructions to return to your application from your php functions. the xml is parsed by xajax message pump and the instructions tell xajax how to update the content and state of your application. the xajaxresponse class currently offers a number of useful commands, such as assign, which sets the specified attribute of an element in your page; append, which appends data to the end of the specified attribute of an element in your page; prepend, which prepends data to the beginning of the specified attribute of an element in your page; replace, which searches for and replaces data in the specified attribute of an element in your page; script, which runs the supplied javascript code; and alert, which shows an alert box with the supplied message text.
xajax最獨特的長處也許就是 xajaxresponse class了。其它的ajax庫需要你親自寫用js寫回調(diào)的句柄,來處理一個異步請求而且得到的數(shù)據(jù),并更新其內(nèi)容。另一方面,xajax只需你簡單的控制好php的內(nèi)容。然后通過xajaxresponse 類,使在你的php函數(shù)中創(chuàng)建xml指令返回給你的程序。xml將被 xajax的信息(pump)解析。其指令告知xajax將如何更新內(nèi)容和你程序中的位置。現(xiàn)在xajaxresponse 已經(jīng)提供了大量并有幫助的指令:http://www.flaspx.com/weblog/blog.php?bid=16 (略...付上詳細的xajaxresponse 類說明)
a single xml response may contain multiple commands, which will be executed in the order they were added to the response. for example, let's say that a user clicks on a button in your application. the onclick event calls the javascript wrapper for a php function. that wrapper sends an asynchronous request to the server through xmlhttprequest where xajax calls the php function. the php function does a database lookup, some data manipulation, or serialization. you use the xajaxresponse class to generate an xajax xml response containing multiple commands to send back to the xajax message pump to be executed:
一個單獨xml響應(yīng)可以包含多條命令,他們將依據(jù)加入響應(yīng)的順序來被執(zhí)行。舉個例子吧,讓我們假設(shè)一個用戶在你的程序中按下了一個按鈕。這個按下的事件將調(diào)用被js封裝好的php函數(shù)。這個封包通過 xmlhttprequest 發(fā)出了一個異步請求給服務(wù)器,讓xajax調(diào)用php函數(shù)。這個php函數(shù)做了一個查詢數(shù)據(jù)庫,一些數(shù)據(jù)處理或排序的操作。而你要用 xajaxresponse 類來產(chǎn)出一個 xajax 的xml響應(yīng),它包含了多條命令。送給xajax 信息pump來執(zhí)行:
objresponse = new xajaxresponse();
objresponse->addassign("myinput1","value",datafromdatabase);
objresponse->addassign("myinput1","style.color","red");
objresponse->addappend("mydiv1","innerhtml",datafromdatabase2);
objresponse->addprepend("mydiv2","innerhtml",datafromdatabase3);
objresponse->addreplace("mydiv3","innerhtml","xajax","<strong>xajax</strong>");
objresponse->addscript("var x = prompt(\"enter your name\");");
return objresponse->getxml();
the xajax message pump would parse the xml message and perform the following:
xajax信息pump將會解析下列xml信息,并執(zhí)行以下操作:
the value of the element with id myinput1 would be assigned to the data in datafromdatabase.
將變量datafromdatabase賦值給id為myinput1的value元素。
the color of the text in the element with id myinput1 would be changed to red.
id為myinput1的字體顏色元素將被換成紅色.
the data in datafromdatabase2 would be appended to the innerhtml of the element with id mydiv1.
datafromdatabase2,此數(shù)據(jù)將被追加到id為mydiv1的innerthml元素的結(jié)束部位
the data in datafromdatabase3 would be prepended to the innerhtml of the element with id mydiv2.
datafromdatabase3,此數(shù)據(jù)將被添加到id為mydiv2的innerthml元素的開始部位
all occurrences of "xajax" in the innerhtml of the element with id mydiv3 would be replaced with "xajax"; making all of the instances of the word xajax appear bold.
id為mydiv3的innerhtml元素中所有的 "xajax" 將被替換成 "xajax",使所有的xajax以粗體顯示。
a prompt would be displayed asking for the user's name and the value returned from the prompt would be placed into a javascript variable named x.
會有一個輸入框彈出,并詢問用戶姓名。從輸入框取得的變量將轉(zhuǎn)換成js變量并命名為x。
all of this is implemented on the server side in the php function by forming and returning an xajax xml response.
所有這些組成了php函數(shù)在服務(wù)器端被執(zhí)行,然后傳回一個xml響應(yīng)。
分享:AJAX之xmlHttpscript type=text/javascript language=javascript !-- //以XML求取數(shù)據(jù) function XmlPost(theEmail) { var webFileUrl = ../User/CheckUser.aspx?LogonName= + theEmail; var result = ; if (window.ActiveXObject) { xmlHttp = new ActiveXObject(
- Ajax中瀏覽器的緩存問題解決方法
- AJAX和WebService實現(xiàn)省市縣三級聯(lián)動具體代碼
- ajax 登錄功能簡單實現(xiàn)(未連接數(shù)據(jù)庫)
- AJAX和WebService實現(xiàn)郵箱驗證(無刷新驗證郵件地址是否合法)
- AJAX和三層架構(gòu)實現(xiàn)分頁功能具體思路及代碼
- 使用AJAX返回WebService里的集合具體實現(xiàn)
- AJAX獲取服務(wù)器當前時間及時間格式輸出處理
- ajax傳遞多個參數(shù)具體實現(xiàn)
- ajax傳遞一個參數(shù)具體實現(xiàn)
- 滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實現(xiàn)探討
- jQery ajax——load()方法示例介紹
- jQuery+Ajax實現(xiàn)表格數(shù)據(jù)不同列標題排序(為表格注入活力)
- 相關(guān)鏈接:
- 教程說明:
AJAX教程-如何十分鐘學(xué)會 xajax
。