Home >Web Front-end >JS Tutorial >AJAX wrapper class usage guide

AJAX wrapper class usage guide

亚连
亚连Original
2018-05-25 14:44:311475browse

This article mainly introduces the guide to the use of AJAX encapsulation classes. Friends who need it can refer to it.

AJAX feels difficult to talk about, but if you encapsulate it, you will find that it is very simple to use, of course. Simple applications, such as message board applications, etc. First of all, let me give you a gift, which is a packaged AJAX class. The download address is http://xiazai.jb51.net/201412/yuanma/ajax3.0(jb51 .net).rar Download this class here and teach you how to use it!
Example!

<html>
 <head>
  <title>Ajax实例</title>
  <script src="ajax3.0.js"></script>
 </head> 
 <body>
  <script>
   document.write(new Date()+"<br>");
   document.write(new Date()+"<br>");
   document.write(new Date()+"<br>");
  </script> 
  <p id="show" style="background:yellow;border:1px solid blue">
   内容加载中..... 
  </p>
  <script>
   function read() {
    Ajax().get("read.php?num="+Math.random(), function(data){
     document.getElementById("show").innerHTML=data;   
    }) 
   }
   read();
   setInerval("read()",3000);
   function send() {
    var username=document.frm.username;
    var desn=document.frm.desn;
    var datao = {username:username.value, desn:desn.value};
    Ajax().post("save.php", datao, function(data){
      
      read();
      username.value="";
      desn.value="";
    });
   }
  </script>
  <form name="frm">
   用户名:<input type="text" name="username" value=""><br>
   内容: <textarea cols="40" rows="5" name="desn"></textarea>
   <input type="button" onclick="send()" value="留言">
  </form>
  <script>
   document.write(new Date()+"<br>");
   document.write(new Date()+"<br>");
   document.write(new Date()+"<br>");
  </script>
 </body>
</html>

First add our ajax3.0.js file, and then we will write this Ajax().post("save.php", datao, function(data))
ajax( ) The first of the two parameters returns xml json or html. The second one is true for asynchronous transmission and false for synchronous transmission. You will understand after the post.
The bottom is the php file

//read.php
@readfile("demo.txt");
//save.php
// header("Content-Type:text/html;charset=gb2312")
$username=$_POST["username"];
$desn = $_POST["desn"];
$fh = fopen("demo.txt", "a");
$text=$username."--".date("Y-m-d H:i:s")."说:".$desn."<br>";
fwrite($fh, $text);
fclose($fh);
//demo.text
fdsafdsa--2012-11-01 12:24:04说:fdsafdsa<br>fdsafdsa--2012-11-01 12:24:07说:fdsafdsafsa<br>111--2012-11-01 12:25:07说:2222<br>eeee--2012-11-01 12:25:10说:eeee<br>fdsfds--2012-11-01 12:25:12说:fdsfdsfds<br>fdsfds--2012-11-01 12:25:19说:fdsfdsfs<br>fdsfdsf--2012-11-01 12:25:22说:sfdsfdsfds<br>fdsafdsa--2012-11-01 12:26:00说:fdsafdsa<br>

The above is what I compiled for everyone. Yes, I hope it will be helpful to everyone in the future.

Related articles:

How to solve the 403 error when django uses ajax post data

A brief analysis of IE’s caching problem for Ajax request results

A detailed explanation of the use of various AJAX methods

The above is the detailed content of AJAX wrapper class usage guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn