Ajax 本身本來不是一門技術,而是在2005年由Jesse James Garrett首創的描述為一個「新」途徑來應用許多已存在的技術,包括:HTML 或 XHTML, Cascading Style Sheets, JavaScript, The Document Object Model, XML, XSLT, 和最重要的 XMLHttpRequest object。
#不刷新頁面請求資料
httpRequest.onreadystatechange = nameOfTheFunction;
<code>httpRequest.onreadystatechange = function(){<br> // Process the server response here.<br>};</code>
<code>httpRequest.open('GET', 'http://www.example.org/some.file', true);<br>httpRequest.send();</code>
"name=value&anothername="+encodeURIComponent(myVar)+"&so=on"
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.onreadystatechange = nameOfTheFunction;
<code>if (httpRequest.readyState === XMLHttpRequest.DONE) {<br> // Everything is good, the response was received.<br>} else {<br> // Not ready yet.<br>}</code>
0 (uninitialized) 或 (請求未初始化)
#1 (loading) 或 (伺服器建立連線)
2 (loaded) or (請求被接收)
3 (interactive) or (執行請求)
4 (complete)或 (request finished and response is ready請求完成回應到位)
Value | #Description | |
0 | UNSENT | Client 有 been created. open() not called yet. |
##1 | OPENED | open() has been called. |
2 | HEADERS_RECEIVED | #send() has been called, and headers and status are available. |
#3 | LOADING | Downloading; responseText holds partial data. |
<code>if (httpRequest.status === 200) {<br> // Perfect!<br>} else {<br> // There was a problem with the request.<br> // For example, the response may have a 404 (Not Found)<br> // or 500 (Internal Server Error) response code.<br>}</code>
httpRequest.responseText – 返回服务器响应字符串
httpRequest.responseXML – 返回服务器响应XMLDocument 对象 可以传递给JavaScript DOM 方法
<code><button id="ajaxButton" type="button">Make a request</button><br><br><script><br>(function() {<br> var httpRequest;<br> document.getElementById("ajaxButton").addEventListener('click', makeRequest);<br><br> function makeRequest() {<br> httpRequest = new XMLHttpRequest();<br><br> if (!httpRequest) {<br> alert('Giving up :( Cannot create an XMLHTTP instance');<br> return false;<br> }<br> httpRequest.onreadystatechange = alertContents;<br> httpRequest.open('GET', 'test.html');<br> httpRequest.send();<br> }<br><br> function alertContents() {<br> if (httpRequest.readyState === XMLHttpRequest.DONE) {<br> if (httpRequest.status === 200) {<br> alert(httpRequest.responseText);<br> } else {<br> alert('There was a problem with the request.');<br> }<br> }<br> }<br>})();<br></script></code>
用户点击"Make a request” 按钮;
事件调用 makeRequest() 方法;
请求发出,然后 (onreadystatechange)执行传递给 alertContents();
alertContents() 检查响应是否 OK, 然后 alert() test.html 文件内容。
<code>function alertContents() {<br> try {<br> if (httpRequest.readyState === XMLHttpRequest.DONE) {<br> if (httpRequest.status === 200) {<br> alert(httpRequest.responseText);<br> } else {<br> alert('There was a problem with the request.');<br> }<br> }<br> }<br> catch( e ) {<br> alert('Caught Exception: ' + e.description);<br> }<br>}</code>
<code><?xml version="1.0" ?><br><root><br> I'm a test.<br></root></code>
<code>...<br>onclick="makeRequest('test.xml')"><br>...</code>
<code>var xmldoc = httpRequest.responseXML;<br>var root_node = xmldoc.getElementsByTagName('root').item(0);<br>alert(root_node.firstChild.data);</code>
<code><label>Your name: <br> <input type="text" id="ajaxTextbox" /><br></label><br><span id="ajaxButton" style="cursor: pointer; text-decoration: underline"><br> Make a request<br></span></code>
<code> document.getElementById("ajaxButton").onclick = function() { <br> var userName = document.getElementById("ajaxTextbox").value;<br> makeRequest('test.php',userName); <br> };</code>
<code>function makeRequest(url, userName) {<br><br> ...<br><br> httpRequest.onreadystatechange = alertContents;<br> httpRequest.open('POST', url);<br> httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');<br> httpRequest.send('userName=' + encodeURIComponent(userName));<br> }</code>
<code>function alertContents() {<br> if (httpRequest.readyState === XMLHttpRequest.DONE) {<br> if (httpRequest.status === 200) {<br> var response = JSON.parse(httpRequest.responseText);<br> alert(response.computedString);<br> } else {<br> alert('There was a problem with the request.');<br> }<br> }<br>}</code>
<code>$name = (isset($_POST['userName'])) ? $_POST['userName'] : 'no name';<br>$computedString = "Hi, " . $name;<br>$array = ['userName' => $name, 'computedString' => $computedString];<br>echo json_encode($array);</code>
以上是什麼是ajax?關於ajax的具體介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!