设为首页 收藏本站
查看: 583|回复: 0

[经验分享] Ajax 入门

[复制链接]

尚未签到

发表于 2017-4-9 12:43:05 | 显示全部楼层 |阅读模式
  <!--  [if gte mso 9]><xml><w:WordDocument><w:View>Normal</w:View><w:Zoom>0</w:Zoom><w:TrackMoves/><w:TrackFormatting/><w:PunctuationKerning/><w:DrawingGridVerticalSpacing>7.8 pt</w:DrawingGridVerticalSpacing><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:SpaceForUL/><w:BalanceSingleByteDoubleByteWidth/><w:DoNotLeaveBackslashAlone/><w:ULTrailSpace/><w:DoNotExpandShiftReturn/><w:AdjustLineHeightInTable/><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="&#45;-"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/><m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]--><!--  [if gte mso 9]><xml><w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"DefSemiHidden="true" DefQFormat="false" DefPriority="99"LatentStyleCount="267"><w:LsdException Locked="false" Priority="0" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Normal"/><w:LsdException Locked="false" Priority="9" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="heading 1"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/><w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/><w:LsdException Locked="false" Priority="39" Name="toc 1"/><w:LsdException Locked="false" Priority="39" Name="toc 2"/><w:LsdException Locked="false" Priority="39" Name="toc 3"/><w:LsdException Locked="false" Priority="39" Name="toc 4"/><w:LsdException Locked="false" Priority="39" Name="toc 5"/><w:LsdException Locked="false" Priority="39" Name="toc 6"/><w:LsdException Locked="false" Priority="39" Name="toc 7"/><w:LsdException Locked="false" Priority="39" Name="toc 8"/><w:LsdException Locked="false" Priority="39" Name="toc 9"/><w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/><w:LsdException Locked="false" Priority="10" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Title"/><w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/><w:LsdException Locked="false" Priority="11" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/><w:LsdException Locked="false" Priority="22" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Strong"/><w:LsdException Locked="false" Priority="20" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/><w:LsdException Locked="false" Priority="59" SemiHidden="false"UnhideWhenUsed="false" Name="Table Grid"/><w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/><w:LsdException Locked="false" Priority="1" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading Accent 1"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List Accent 1"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid Accent 1"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/><w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/><w:LsdException Locked="false" Priority="34" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/><w:LsdException Locked="false" Priority="29" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Quote"/><w:LsdException Locked="false" Priority="30" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List Accent 1"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List Accent 1"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading Accent 2"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List Accent 2"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid Accent 2"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List Accent 2"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List Accent 2"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading Accent 3"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List Accent 3"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid Accent 3"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List Accent 3"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List Accent 3"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading Accent 4"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List Accent 4"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid Accent 4"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List Accent 4"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List Accent 4"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading Accent 5"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List Accent 5"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid Accent 5"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List Accent 5"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List Accent 5"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/><w:LsdException Locked="false" Priority="60" SemiHidden="false"UnhideWhenUsed="false" Name="Light Shading Accent 6"/><w:LsdException Locked="false" Priority="61" SemiHidden="false"UnhideWhenUsed="false" Name="Light List Accent 6"/><w:LsdException Locked="false" Priority="62" SemiHidden="false"UnhideWhenUsed="false" Name="Light Grid Accent 6"/><w:LsdException Locked="false" Priority="63" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/><w:LsdException Locked="false" Priority="64" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/><w:LsdException Locked="false" Priority="65" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/><w:LsdException Locked="false" Priority="66" SemiHidden="false"UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/><w:LsdException Locked="false" Priority="67" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/><w:LsdException Locked="false" Priority="68" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/><w:LsdException Locked="false" Priority="69" SemiHidden="false"UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/><w:LsdException Locked="false" Priority="70" SemiHidden="false"UnhideWhenUsed="false" Name="Dark List Accent 6"/><w:LsdException Locked="false" Priority="71" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/><w:LsdException Locked="false" Priority="72" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful List Accent 6"/><w:LsdException Locked="false" Priority="73" SemiHidden="false"UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/><w:LsdException Locked="false" Priority="19" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/><w:LsdException Locked="false" Priority="21" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/><w:LsdException Locked="false" Priority="31" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/><w:LsdException Locked="false" Priority="32" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/><w:LsdException Locked="false" Priority="33" SemiHidden="false"UnhideWhenUsed="false" QFormat="true" Name="Book Title"/><w:LsdException Locked="false" Priority="37" Name="Bibliography"/><w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/></w:LatentStyles></xml><![endif]--><!--  [if gte mso 10]><mce:style><!--/* Style Definitions */table.MsoNormalTable{mso-style-name:"Table Normal";mso-tstyle-rowband-size:0;mso-tstyle-colband-size:0;mso-style-noshow:yes;mso-style-priority:99;mso-style-qformat:yes;mso-style-parent:"";mso-padding-alt:0cm 5.4pt 0cm 5.4pt;mso-para-margin:0cm;mso-para-margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}--><!-- [endif]-->
Getting started with AJAX using PHP : Tutorial
By : Mamun Zaman

Translated by: garcon1986


AJAX stands for Asynchronous JavaScript AndXML. Any server side technology that supports JavaScript also supports AJAX.AJAX is a browser technology, and is therefore independent of web serverplatforms.
AJAX代表了异步Javascript和XML。任何支持Javascript的服务器端技术也同样支持AJAX。AJAX是一种浏览器技术,因此也独立于网络服务器平台。

In this article we will learn about whatAJAX is, how it works, and how can we use AJAX with PHP. Please remember, AJAXis not a programming language, so you don’t have to learn any new technology.AJAX can be implemented by using existing standards (JavaScript and XML) in adifferent way.
在这篇文章中,我们会了解到什么是AJAX,AJAX如何工作以及在PHP中如何使用AJAX。请牢记:AJAX不是一门编程语言,因此你并不需要学习任何一门新技术。AJAX能通过现行的标准(Javascript和XML)以另一种方式来实现。

If we are using PHP or any server sidetechnology and need to extract data from storage on a server (eg a database ora file), we will have to make an HTTP request (either POST or GET) to get thedata. Once the data is received the web page will need to be reloaded to show thedata. Using AJAX technology we can request and receive the data from server inbackground and then display it on the page without a reload. AJAX uses HTTPrequests for this. With AJAX, JavaScript communicates directly with the server,through the JavaScript XMLHttpRequest object (XML over HTTP). With an HTTPrequest, a web page can make a request to, and get a response from a web serverwithout reloading the page.
如果我们使用PHP或者其他任何服务器端技术,并需要从一个服务器存储(数据库或文件)中提取数据,我们就必须通过一个HTTP 请求(POST或GET)来获得这些数据。一旦接收完数据,网页需要重新载入来显示数据。通过AJAX技术我们能从服务器端请求并接收数据,然后无需重载就能在页面中显示出来。这个过程中,AJAX使用HTTP请求。通过AJAX, Javascript使用XMLHttpRequest对象(XML over HTTP)直接和服务器交流。通过HTTP请求,网页不需重载就能从服务器端请求并返回结果。

The XMLHttpRequest object is supported inInternet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, andNetscape 7. But the creation of XMLHttpRequest object is different in InternetExplorer than the other browsers. I will discuss this later. To use AJAX torequest a data from the server we need to do the following.
IE 5.0+, Safari 1.2, Mozilla 1.0/Firefox,Opera 8+以及Netscape7都支持XMLHttpRequest对象。但IE中XMLHttpRequest对象的创建不同于其他浏览器。这一点在下面会讨论到。为了从服务器端请求数据,我们需要做以下的工作:

Create an XMLHttpRequest object.
2. Then using this object, request datafrom the server.
3. JavaScript will then monitor for thechanging of state of the request.
4. If the response is successful, then thecontent from the data store requested will be returned as response (responsecan be in the form of a String or XML).
5. Use the response in your web page.
创建一个XMLHttpRequest对象
使用这个对象从服务器请求数据
Javascript会监控请求状态的改变
如果响应成功,向数据库请求的内容将被返回,返回的形式是字符串或XML
网页中调用响应结果

1. Create an XMLHttpRequest object

JavaScript has a built-in XMLHttpRequestobject. You can use that for Firefox, Safari, and Opera. For Internet Exploreruse the ActiveXObject, there is also a difference between IE 5.0 and IE 6.0+ inhow to create the object. The following codes create an XMLHttpRequest for allbrowsers:
Javascript有一个内置的XMLHttpRequest对象,它能在Firefox, Safari, 和 Opera中使用。IE使用ActiveXObject, 但IE5.0和IE6.0+在创建XMLHttpRequest对象时也有所不同。以下代码适用于所有浏览器:

var req;

if(window.XMLHttpRequest){
//For Firefox, Safari, Opera
req = new XMLHttpRequest();
}
else if(window.ActiveXObject){
//For IE 5
req = newActiveXObject("Microsoft.XMLHTTP");
} else if(window.ActiveXObject){
//For IE 6+
req = new ActiveXObject("Msxml2.XMLHTTP");
}
else{
//Error for an old browser
alert('Your browser is not IE 5 or higher,or Firefox or Safari or Opera');
}

Here, first we are using the built-inJavaScript function XMLHttpRequest() for creating an XMLHttpRequest for Firefox,Safari and Opera. If the browser does support window.ActiveXObject, then it isInternet Explorer. For IE versions 5.0+, use newActiveXObject("Microsoft.XMLHTTP") and for IE 6.0+ use newActiveXObject("Msxml2.XMLHTTP"). If the browser does not support thebuilt-in JavaScript function XMLHttpRequest() or ActiveXObject, then it doesnot support AJAX. You can also use JavaScript try-catch blocks for the sameoutput.
首先,我们使用一个内置的Javascript 函数XMLHttpRequest()来为FF, Safari和opera创建一个XMLHttpRequest请求。如果这个浏览器支持window.ActiveXObject,则它是IE。对于IE5.0+版本,使用 newActiveXObject(“Microsoft.XMLHTTP”);对于IE6.0+版本,使用 new ActiveXObject(“Msxml2.XMLHTTP”)。如果浏览器不支持XMLHttpRequest()or ActiveXObject, 则无法支持AJAX。你也能使用Javascript try-catch 块来得到相同的输出结果。
如下:
var req;
try
{
// Firefox, Opera, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=newActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert('Your browser is not IE 5 or higher,or Firefox or Safari or Opera');
}
}
}

In JavaScript, if statements within a trysection fail, then the execution resumes from the corresponding catch block.Here first we are trying to get create a XMLHttpRequest using the built-infunction, and if it fails then we will try usingActiveXObject("Msxml2.XMLHTTP"), and if it fails also we will tryActiveXObject("Microsoft.XMLHTTP"). If all these fail, then we willalert the user that his/her browser does not support AJAX.
在Javascript中,如果try部分的语句失败,则执行相对应的catch块。首先,我们试着用内置函数创建XMLHttpRequest对象,如果失败,我们试着使用ActiveXObject("Msxml2.XMLHTTP"),如果再失败,我们试着使用ActiveXObject("Microsoft.XMLHTTP")。如果以上都失败了,我们显示一个警告来告知用户他/她的浏览器不支持AJAX。

2. Request for a web page

After creating the XMLHttpRequest we nowneed to send the web request using the open method. We also need to specify theHttpRequest method, GET or POST. Use the following code to send the request.
创建XMLHttpRequest之后,我们使用open()方法来发送网页请求。必须指定HttpRequest方法,GET或者POST。使用以下代码发送请求:

req.open(“GET”,”somedata.php”);
req.send(null);

Here, req is the XMLHttpRequest object. Itwill request to the server for somedata.php using GET method. The open functionalso has a third parameter, an optional boolean parameter. You should set thatto true :
这里req是XMLHttpRequest对象。它会通过GET方法向服务器请求somedata.php的数据。Open()函数可以包含第三个参数,一个可选的布尔值(boolean)参数,并且应该把它设为真(true)。

req.open(“GET”,”somedata.php”,true);
req.send(null);

Both of the above is correct.
以上两个例子都是正确的。

3. Monitor for the response of the request

You will need to monitor for the state ofthe request. For doing this you can assign a function to req.onreadystatechange(Here, req is the XMLHttpRequest object), like below.
为了监控请求的状态,你需要赋给req.onreadystatechange一个函数,就像下面:
req.onreadystatechange=function()
{
if(req.readyState==4 && req.status ==200)
{
var resp = req.responseText;
}
}

Or like this,
或者:
req.onreadystatechange = handleResponse;

function handleResponse(){
if(req.readyState == 4 &&req.status == 200){
//returned text from the PHP script
var response = req.responseText;
}
}

The readyState property holds the status ofthe server's response. Each time the readyState changes, the onreadystatechangefunction will be executed. Here are the possible values for the readyStateproperty:
readyState属性持有服务器响应的状态。每次readyState改变,onreadystatechange函数都会执行。以下是readyState的几种值:
State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is complete

And status is the status of the HTTPRequest, like 500 Internal Server Error, 400 Bad Request, 401 Unauthorized, 403Forbidden, 404 Not Found etc. 200 means no error.
这里的状态是指HTTP请求的状态,就像500是内部服务器错误,400是坏的请求,401是未验证,403禁止,404未发现等等。200意味着ok。

4. Get the response

The response will be as string or as XML.The data sent back from the server can be retrieved with the responseTextproperty as string. Use responseXML for getting the response as XML.
响应将以字符串或XML的形式出现。从服务器发回来的数据能通过responseText以字符串的形式获得,或者通过responseXML以XML的形式获得。
5. Use the response on your web page

You can use the response you got from theXMLHttpRequest in your web page/application. You can either set a value of atext field or use the returned HTML from the web request as innerHTML for a<div></div> tag or <span></span> tag (See below for theimplementation of this)
你能将从XMLHttpRequest得到的响应运用到网页或应用程序中。你能设置文本区域的值或者使用请求返回的HTML作为div或span 标签的innerHTML内容。(请看下面的实现方法)

Using AJAX with PHP

I usually place all the AJAX relatedfunctions in one JavaScript (ajax.js), and later add the JavaScript in my PHPpages. My ajax.js looks like below.
我经常把关于AJAX的函数放到一个Javascript文件(ajax.js)里,之后加到php页面里。

function createRequestObject(){

var req;

if(window.XMLHttpRequest){
//For Firefox, Safari, Opera
req = new XMLHttpRequest();
}
else if(window.ActiveXObject){
//For IE 5+
req = newActiveXObject("Microsoft.XMLHTTP");
}
else{
//Error for an old browser
alert('Your browser is not IE 5 or higher,or Firefox or Safari or Opera');
}

return req;
}

//Make the XMLHttpRequest Object
var http = createRequestObject();

function sendRequest(method, url){
if(method == 'get' || method == 'GET'){
http.open(method,url,true);
http.onreadystatechange = handleResponse;
http.send(null);
}
}

function handleResponse(){
if(http.readyState == 4 &&http.status == 200){
var response = http.responseText;
if(response){
document.getElementById("ajax_res").innerHTML= response;
}
}
}

Now I add a <script></script>tag in my PHP pages like following to access the JavaScript functions.
现在我添加一个<script>标签到PHP页面来访问javascript函数。

Then I create a function called sendReq()as shown above for preparing the URL to the PHP page to get some data from thedatabase. If you look into the ajax.js, you will see that I’m assigning theoutput of the PHP page to a <div> tags innerHTML. My div tag has the id=’ajax_res’.
然后,我创建一个叫做sendReq()的函数来准备php页面的URL从数据库中获得数据。如果你仔细看ajax.js,你会发现我付给了php页面的输出给了一个div标签。这个div标签的id是ajax_res。

<div align="center"class="black" id="ajax_res">
<img src="wt_bg.jpg">
</div>

After calling the sendReq function with avalue of ‘success’, it will connect to the server for sending a request to theURL get_lcm.php?status=success, then get the data from the database using thevalue ‘success’, and set the innerHTML attribute of the div tag whose id is‘ajax_res’ according to the response. Initially my div tag shows an image, butafter AJAX responds successfully this image will be replaced with the response.You can also get the response as the value of an input text.
通过success值成功调用sendReq函数之后,它会由发送请求到URLget_lcm.php?status=success连接到数据库,然后使用success值从数据库中获得数据,并根据响应设置id是ajax_res的div标签的innerHTML属性。最初我的div标签显示一张图片,但是AJAX响应成功之后,这张图片会被响应代替。你也能获得响应作为输入文本的值。

My get_lcm.php looks like this.

<?php
include_once("create_table.php");

if(!empty($status)){
echo "<strong>Search Result forStatus: ".$status."</strong><br><br>";

$ct = new createtable("select * fromlcm where state=$status");

$ct->table_viewer();
}
?>

I used one of my PHP classes‘create_table.php’ for creating a table from SQL queries. ‘create_table.php’has a function, table_viewer(), to print the SQL output as a HTML table. I willdiscuss about this class and some other data abstraction classes I used in somelater articles, if necessary.
我使用了我的一个php类create_table.php 来通过sql语句创建一个表格。Create_table.php有一个函数,table_viewer(), 以一个HTML表格的形式打印SQL输出。如果需要的话,我会讨论到我在其他文章中用过的类和其他数据抽象类。
I had the following HTML code for callingthe AJAX function using the sendReq() as described earlier.
像上面所描述的,我通过sendReq() 来调用AJAX函数。
<a class="none"href="#" >ACTIVE</a>

If a user clicks the above hyperlink, thenthe JavaScript function sendReq is invoked using the value of status equal to1. After that the webpage with URL get_lcm.php?status=1 will be invoked. ThisPHP will get the information from database and shows the result as table. Thisresult will then be displayed within the <div> tag.
如果一个用户点击了上面的超链接,然后通过status的值等于1,这个javascript函数sendReq被调用。PHP会从数据库中获得信息并显示将结果在表格中。这个结果会在div标签中显示。
Using AJAX from PHP is very easy asdescribed here. All you need is the JavaScript functions for sending the XMLHttpRequestand then handle the Http Response. It will simplify development is you placeall your AJAX related code in a single JavaScript file and reference it fromanywhere you need AJAX.
就像这里描述的一样,使用AJAx是非常简单的。你只需要javascript 函数来发送XMLHttpRequest然后处理Http响应。它会简化开发过程,因为你把所有Ajax相关的代码放到一个单独的javascript文件,并能在任何需要Ajax的地方引用它。
There is another way of getting the sameresult from PHP without using the XMLHttpRequest object. But I personally donot like the concept, because it is missing one of the main ingredients ofAsynchronous JavaScript And XML (AJAX), XML. This concept is good for the oldbrowsers with no supports for XMLHttpRequest object, but as all the newerversions are supporting XMLHttpRequest object you can use AJAX for the commonbrowsers.
还有另外一种方式从php中得到数据,但不是使用XMLHttpRequest对象。但是我个人不喜欢这个概念,因为他缺少了一个很重要的AJAX成分,XML。这个概念对于不支持XMLHttpRequest对象的旧浏览器很好,但是所有的新版本都支持XMLHttpRequest对象。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362422-1-1.html 上篇帖子: 浅谈PHP开发团队治理及程序员做人问题! 下篇帖子: axis2 webservice入门知识(JS,Java,PHP调用实例源码)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表