Asynchronous objects
A) Creating an asynchronous object
b) Set parameters such as the URL of the request
c) Send request
D) Registration time
e) Get the returned content in the registered event and modify the contents of the page display
Boolean type cannot be directly used with echo output
Common response states Ajax concepts
In the browser, we also can not 刷新页面 , through ajax the way to get some new content, similar pages have 微博 , 朋友圈 , 邮箱 etc.
Asynchronous Javascript And XML(Asynchronous JavaScript and XML),
He is not a new technology, but a combination of existing technology: The core is the JS objectXMLHttpRequest
XMLHttpRequest
ajaxUse is still HTTP请求 , then let's recall a complete HTTP请求 need for what
>
-
URL of the request, methodget/post
-
Submit the requested content 数据 , 请求主体 etc.
-
Receive responses back to content
-
Five-step method of use:
-
Creating XMLHttpRequest Objects
-
Registering callback Functions
- When
回应 the server we're up we want to execute what logic
-
Use the Open method to set up basic information for server-side interaction
- Set submissions,
网址 数据 and post submit some additional content
-
Set the data sent to start and server-side interaction
-
Update interface
- In the registered callback function, get the returned data, update the interface
Xmlhttprequest_api explanation
1. CreateXMLHttpRequestObject (compatibility notation)
var xml=new XMLHttpRequest();
var xml=new ActiveXObject("Microsoft.XMLHTTP");
- Consider compatibility to create Ajax objects
var request, if(XMLHttpRequest) { //new browser notation request = new XMLHttpRequest ();} else{ //ie5,ie6 writing request = new ActiveXObject ("Microsoft.XMLHTTP");}
2. Send the request:
| Method |
Description |
| Open (method,url,async) |
Specifies the type of request, the URL, and whether the request is processed asynchronously.
-
- method: type of request; GET or POST
- URL: The location of the file on the server
- Async: True (asynchronous) or false (synchronous)
|
| Send (string) |
Sends the request to the server.
-
- string: only for POST requests
|
3.POST Request Attention Point:
If you want to use the request as the form form submits data POST , you need to use XMLHttpRequest the object's setRequestHeader() method to add the HTTP header. Then add the data you want to send in the Send () method:
Xmlhttp.open ("POST", "ajax_test.php", true); Xmlhttp.setrequestheader ("Content-type", "application/ X-www-form-urlencoded "); Xmlhttp.send (" Fname=bill&lname=gates ") ;
4.onreadystatechange Events
When the server gives us feedback, we need to implement some logic
| Properties |
Description |
| onReadyStateChange |
The function (or function name) is called whenever the ReadyState property is changed. |
| ReadyState |
The state of being xmlhttprequest. Vary from 0 to 4.
-
- 0: Request not initialized
- 1: Server Connection established
- 2: Request received
- 3: In Request processing
- 4: The request is complete and the response is ready
|
| Status |
$: "OK"
404: Page Not Found |
4. Server response Content
If the response is a normal string, use responseText , if the response is XML , use theresponseXML
| Properties |
Description |
| ResponseText |
Gets the response data in the form of a string. |
| Responsexml |
Get the response data in XML form. |
Get data, add it directly to the request url
<script type= "Text/javascript" > //Create XMLHttpRequest object var xml = new XMLHttpRequest (); Set the information xml.open (' Get ', ' 01.ajax.php?name=fox ') that interacts with the server; Xml.send (null); Get request write NULL here //Receive server feedback Xhr.onreadystatechange = function () { //This step is to determine if the server responds correctly if ( Xhr.readystate = = 4 && Xhr.status = = +) { //Print response Content alert (xml.responsetext); } }; </script>
<script type= "Text/javascript" > //Async object var xhr = new XMLHttpRequest (); Set Property Xhr.open (' Post ', ' 02.post.php '); If you want to submit data using post, you must add xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Passing data through the Send method xhr.send (' name=fox&age=18 '); Send and accept return value Xhr.onreadystatechange = function () { //This step is to determine if the server responds correctly if (xhr.readystate = = 4 && xhr . Status = = $) { alert (xhr.responsetext); } }; </script>
Php. 02®. Ajax asynchronous processing, common response states, XMLHttpRequest objects and APIs, Ajax Get/post methods,