JS Original Eco-Ajax Creation Method Introduction (XMLHttpRequest)

Source: Internet
Author: User

XMLHttpRequest

The code is as follows Copy Code

<script type= "Text/javascript" >
function Loadxmldoc ()
{
var xmlhttp;
if (window. XMLHttpRequest)
{//code for ie7+, Firefox, Chrome, Opera, Safari
Xmlhttp=new XMLHttpRequest ();
}
Else
{//code for IE6, IE5
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
Xmlhttp.onreadystatechange=function ()
{
if (xmlhttp.readystate==4 && xmlhttp.status==200)
{
document.getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext;
}
}
Xmlhttp.open ("Get", "Filefunction.php?dt=attatchment&u=abc.txt", true);
Xmlhttp.send ();
}
</script>

Here's a look at jquery+ajax/"target=" _blank ">jquery Ajax applications

JQuery Ajax Events

Ajax requests produce a number of different events, and we can subscribe to these events and process our logic in them. There are two kinds of Ajax events in jquery: Local events and global events.

A local event is defined within a method at each AJAX request, for example:

The code is as follows Copy Code

$.ajax ({
Beforesend:function () {
Handle the Beforesend Event
},
Complete:function () {
Handle the Complete event
}
// ...
});

At the same time we can also directly use such as

Jquery.get (URL, [data], [callback]): Asynchronous request using Get method

Load (URL, [data], [callback]): Loads the remote HTML file code and inserts it into the DOM

Jquery.post (URL, [data], [callback], [Type]): Asynchronous request using POST method


These are very good, only suitable for small amount of input if there is a large number of We must use the Ajax-serialize () method method, as follows

The code is as follows Copy Code

Html

<form method= "POST" id= "SubmitForm" name= "SubmitForm" ><tr>
<td><table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
&LT;TD width= "6%" > Mobile phone:</td>
&LT;TD width= "21%" ><input name= "Mo" type= "text" class= "INP" id= "Mo" size= ""/></td>
&LT;TD width= "8%" align= "right" > Mailbox:</td>
&LT;TD width= "21%" ><input name= "Email" type= "text" class= "INP" id= "email" size= "/></td>"
&LT;TD width= "44%" ><span class= "Gray" ></span></td>
</tr>
</table></td>
</tr>
<tr>
&LT;TD height= "align=" "Left" >
<textarea name= "bak" cols= "rows=" 5 "class=" Lab "id=" BAK1 "></textarea></td>
</tr>
<tr>
<td><input name= "button" type= "button" class= "but" id= "Buttonajax" value= "submit Message"/>
<span class= "Gray" > </span></td>
</tr>

</form>

$ (' #buttonajax '). Click (function () {
 //alert ($ (this). Val ());
 //checkem ()
 var PostData = $ (' #submitform '). Serialize ();
 //alert (postdata);
 $.ajax ({
  type: "POST",
  url: "/postajax.php",
  data: PostData,
  success:function (msg) {
   //alert (msg);
   if (msg ==1) {
    alert (' Your question has been submitted successfully! ');     
   
   
  }
 };
})

Related Article

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.