Good Java programmers learn to share routes native of Ajax

Good Java programmers learn to share routes using native Ajax, first let's look at what is AJAX

        AJAX is a without having to reload the entire web page technical section of the page can be updated.

        AJAX is Asynchronous, JavaScript and XML.

        AJAX is a technique for creating fast dynamic web pages.

        By exchanging small amounts of data with the server behind the scenes, AJAX can make asynchronous page updates. This means that, for certain parts of the page to be updated without reloading the entire page.

        Traditional web page (do not use AJAX) If you need to update the entire page surface content, essential overloaded.


AJAX works

    Browser Server Browser server

    Launch event

    Create XMLHttpRequest object

    send request                

                            Sent by a client object receives

                            Reply to a target ResponseText

    Receiving ResponseText results

    Local data update page

                                                      

XMLHttpRequest object

    XMLHttpRequest is the foundation of AJAX.

    All modern browsers support XMLHttpRequest object (IE5 and IE6 use ActiveXObject).

    XMLHttpRequest for exchanging data with the server in the background. This means that, for certain parts of the page to be updated without reloading the entire page.


Create XMLHttpRequest object

    All modern browsers (IE7 +, Firefox, Chrome, Safari and Opera) are built-in XMLHttpRequest object.

    Create XMLHttpRequest object syntax:

    variable=new XMLHttpRequest();


    In order to cope with all modern browsers, including IE5 and IE6, please check whether the browser supports the XMLHttpRequest object. If supported, XMLHttpRequest objects are created. If not, create ActiveXObject:


    ** Examples **

    var xmlhttp;

    if (window.XMLHttpRequest)

    {

        // IE7 +, Firefox, Chrome, Opera, Safari browser code execution

        xmlhttp=new XMLHttpRequest();

    }

    else

    {

        // IE6, IE5 browser code execution

        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    }


AJAX - Request sends a request to the server


    XMLHttpRequest object and the server for exchanging data.

    Send a request to the server

        To send a request to the server, we use the XMLHttpRequest object's open () and send () method:

            xmlhttp.open("GET","ajax_info.txt",true);

            xmlhttp.send();

    open (method, url, async) a predetermined type of request, URL, and whether a request for asynchronous processing.

        method: type of the request; the GET or POST

        url: location of the file on the server

        async: true (asynchronous) or false (synchronous)

    send (string) sends a request to the server.

        string: POST requests only


GET or POST?

    Compared with POST, GET easier and faster, and can be used in most cases.

    However, in the following cases, please use the POST request:

        Can not use the cache files (files or update the database on the server)

        Sending large amounts of data (POST not limit the amount of data) to the server

        When sending the user input contains unknown character, POST is more stable and more reliable than the GET


** GET request **

    

    xmlhttp.open("GET","/try/ajax/demo_get.php",true);

    xmlhttp.send();


    If you want to send information through GET method, add the information to the URL:


    xmlhttp.open("GET","/try/ajax/demo_get2.php?fname=Henry&lname=Ford",true);

xmlhttp.send();


** POST request **

    

    A simple POST request:


    xmlhttp.open("POST","/try/ajax/demo_post.php",true);

    xmlhttp.send();

    

    If you need an HTML form POST data as like, use setRequestHeader () to add the HTTP header. Then the provisions of data you want to send in send () method:


    xmlhttp.open("POST","/try/ajax/demo_post2.php",true);

    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    xmlhttp.send("fname=Henry&lname=Ford");


AJAX - server response


    Please perform the functions specified in response to a state of readiness onreadystatechange event:


    To obtain a response from the server responds to the server, use the XMLHttpRequest object's responseText or responseXML property.

    responseText response data string obtained.

    responseXML obtained in the form of XML response data.


Guess you like

Origin blog.51cto.com/14479068/2432049