Rumah >hujung hadapan web >tutorial js >什么是ajax?关于ajax的具体介绍
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>
open() 的第一个参数是HTTP 请求的方法 – GET, POST, HEAD, 或者其他服务器支持的方法。方法名全部大写是http标准,不然有些浏览器(例如:Firefox)可能会不发器请求。 点击 W3C specs 获得更多关于http请求方法的信息。
第二个参数是要请求的url。从安全方面考虑,默认不能跨域请求url。确保所有页面在同一个域名下,不然调用open()方法,你会得到 "permission denied” 错误。 一个常见的跨域是你网站的域名是 domain.tld,但是尝试用 www.domain.tld访问页面。如果真的想跨域请求,查看 HTTP access control。
可选的第三个参数设置这个请求是同步还是异步的。如果是true (默认值), JavaScript 会继续执行,用户操作页面的同时,服务器返回数据。这是 AJAX的A。
"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) or (请求未初始化)
1 (loading) or (服务器建立连接)
2 (loaded) or (请求被接收)
3 (interactive) or (执行请求)
4 (complete) or (request finished and response is ready请求完成响应到位)
Value | State | Description |
0 | UNSENT | Client has 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. |
4 | DONE | The operation is complete. |
<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>
Atas ialah kandungan terperinci 什么是ajax?关于ajax的具体介绍. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!