Php. 02®. Ajax asynchronous processing, common response states, XMLHttpRequest objects and APIs, Ajax Get/post methods,

Source: Internet
Author: User

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.

    • Word Explanation:

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:

      1. Creating XMLHttpRequest Objects

      2. Registering callback Functions

        • When 回应 the server we're up we want to execute what logic
      3. Use the Open method to set up basic information for server-side interaction

        • Set submissions, 网址 数据 and post submit some additional content
      4. Set the data sent to start and server-side interaction

        • Send data
      5. Update interface

        • In the registered callback function, get the returned data, update the interface

Xmlhttprequest_api explanation

1. Create XMLHttpRequestObject (compatibility notation)
    • new Browser version:
var xml=new XMLHttpRequest();
    • (IE5 and IE6)
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.

    • Sample code: GET

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>
    • Sample code: POST
<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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.