Use native Ajax to access back-end data and present it in the front page (small rookie himself to organize to play, big God do not spray)

Source: Internet
Author: User
Tags dreamweaver

  1. First you need to have PHP environment, about the PHP environment, the construction of PHP local site, this is not repeated here please see here: http://www.cnblogs.com/Gabriel-Wei/p/5950465.html
    We opened the Wamp and created a new Phpstudy site in Dreamweaver.
    This article we need two files, a test.html front-end page, a tigong.php Data supply page, the following are described separately.
  2. tigong.php
    Below the Dreamweaver,phpstudy site, we create a new tigong.php page with the following content:
    1 <? PHP 2 echo"Hello Tom"; 3 ?>

    In order to let the inside support Chinese, we add this sentence header ("Content-type:text/html;charset=utf-8"), at this time the tigong.php as follows

    <? PHP Header ("Content-type:text/html;charset=utf-8"); echo"Hello Li Ming";? >


    Run with our browser,

    The Address bar information address copy, with postman test data, if you do not know what is postman, please Baidu, after the data interaction article will always use to postman. (In short, postman is testing the interface to receive data normally),

    The tigong.php file is ready, so let's write our front page.



  3. Test.html we set to click the button to get the data
    <!DOCTYPE HTML><HTML><Head><Scripttype= "Text/javascript">window.onload=function(){     varBT1=document.getElementById ("BT1"); varDiv1=document.getElementById ("Div1"); //bind the event to BT1, and one will do the onload after the data is loadedBt1.onclick=function(){         //First step: Create an Ajax object         //Write compatible         if(window. XMLHttpRequest) {varxmlHttp=NewXMLHttpRequest (); }Else{                               //compatible with IE6                 varxmlHttp=NewActiveXObject ("Microsoft.XMLHTTP")                 }         //Step Two: Get the data you want to broadcast. This example does not         //Step Three: Set the way to send and request the destination addressXmlhttp.open ("GET","tigong.php"); //The third parameter does not write by default is asynchronous                  //Fourth Step: Set the encoding of the requestXmlhttp.setrequestheader ("Content-type","application/x-www-form-urlencoded"); //Fifth Step: monitoring the entire process, binding onreadystatechange events, a total of four times 0-1.1-2.2-3.3-4, we need 4 andXmlhttp.onreadystatechange=function(){             if(Xmlhttp.readystate==4 &&Xmlhttp.status== $){                 //back-end data output What, our responsetext get whatdiv1.innerhtml=Xmlhttp.responsetext;        }             }; //Sixth step: Send the requestXmlhttp.send ();              }; };</Script><MetaCharSet= "Utf-8"><style>#div1{background:#f00;Color:#fff;Font-weight:Bold;width:200px;Height:200px;     }img{width:200px;    }</style><title>Ajax</title></Head><DivID= "Div1">  <!--in the data did not come, we show a picture of loading, Baidu search loading a big push -  <imgsrc= "Images/1.gif"> </Div><BR><inputtype= "button"ID= "BT1"value= "Click to get Data"><Body></Body></HTML>




    After clicking:

    Can see that the data inside our PHP page is back.

  4. Take a look at Chrome's development tools
    Select all in the Network Monitor, you can view all the files, if not, refresh the page,


    Click tigong.php



Use native Ajax to access back-end data and present it in the front page (small rookie himself to play, big God do not spray)

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.