How does AJAX work?

前端 未结 8 1879
北海茫月
北海茫月 2020-11-22 10:38

What is the essence of AJAX? For example, I want to have a link on my page such that when a user clicks this link, some information is sent to my server without the reloadin

相关标签:
8条回答
  • 2020-11-22 11:16

    The essence of AJAX is this:

    Your pages can browse the web and update their own content while the user is doing other things.

    That is, your javascript can send asynchronous GET and POST requests (usually via an XMLHttpRequest object) then use the results of those requests to modify its page (via Document Object Model manipulation).

    0 讨论(0)
  • 2020-11-22 11:18

    Ajax is more than reload just a part of the page. Ajax stands for Asynchronous Javascript And Xml.

    The only part of Ajax that you need is the XMLHttpRequest object from javascript. You have to use it to load and reload small part of your html as a div or any other tags.

    Read that example and you will be pro sooner as you think!

    <html>
    <body>
    
    <script type="text/javascript">
    function ajaxFunction()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
      alert("Your browser does not support XMLHTTP!");
      }
    xmlhttp.onreadystatechange=function()
    {
    if(xmlhttp.readyState==4)
      {
      document.myForm.time.value=xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","time.asp",true);
    xmlhttp.send(null);
    }
    </script>
    
    <form name="myForm">
    Name: <input type="text" name="username" onkeyup="ajaxFunction();" />
    Time: <input type="text" name="time" />
    </form>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题