Ajax Introductory Tutorials XMLHttpRequest and $.ajax usage instructions

Source: Internet
Author: User

For Ajax, the core object is XMLHttpRequest, all AJAX operations are inseparable from the operation of this object.
Creating XMLHttpRequest Objects
For IE browsers:

The code is as follows Copy Code

XmlHttp = new ActiveXObject (' microsoft.xmlhttp ');

For other browsers:

The code is as follows Copy Code

XmlHttp = new XMLHttpRequest ();

Different browsers support the XMLHttpRequest object in JavaScript differently, so you need to make a decision based on the situation.

XMLHttpRequest Object-related methods

Open Request

Xmlhttprequest.open (delivery mode, address, asynchronous request)

Ready to execute

Xmlhttprequest.onreadystatechange

Get execution results

Xmlhttprequest.responsetext

An example of a simple php+ajax:
First, the Test.js file:

  code is as follows copy code

var xmlHttp ;
Function S_xmlhttprequest () {
 if (window. ActiveXObject) {
  xmlhttp=new activexobject (' microsoft.xmlhttp ');
 }else if (window. XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest ();
 }
}
function php100 (URL) {
 s_ XMLHttpRequest ();
 xmlhttp.open ("Get", "do.php?id=" +url,true);
 xmlHttp.onreadystatechange=byphp;
 xmlhttp.send (NULL);
}
Function byphp () {
 var byphp100=xmlhttp.responsetext;
 document.getelementbyid (' php100 ' ). innerhtml=byphp100;
}

Then the file that executes the PHP operation, do.php

The code is as follows Copy Code

? Php
$id =@$_get[id];
For ($i =1 $i <10; $i + +) {
echo $id;
}

Then the front end display page, test.html

The code is as follows Copy Code

<script src= "Test.js" type= "Text/javascript" ></script>
<a href= "#" onclick= "php100 (1)" >1</a> | |
<a href= "#" onclick= "php100 (2)" >2</a> | |
<a href= "#" onclick= "php100 (3)" >3</a>
<div id= "php100" ></div>

In jquery Ajax, data is sent to the server in the form of a key-value pair (Key/value), which can be serialized as a key-value pair using the jquery Ajax Serialize () method form when submitting form data using AJAX (key1=value1&key2= Value2 ... ) after submission. The Serialize () method represents a text string using a standard url-encoded encoding. The following is an instance of using serialize () to serialize a form:

JQuery Ajax Prototypes:

The code is as follows Copy Code

$.ajax ({
Type: "POST",
Url:ajaxcallurl,
Data: "Key=value&key2=value2",
Success:function (msg) {alert (msg);}
});


Ajax Serialize ():

The code is as follows Copy Code
$.ajax ({
Type: "POST",
Url:ajaxcallurl,
data:$ (' #formID '). Serialize (),//form to submit
Success:function (msg) {alert (msg);}
});

Serialize () Serialization of form instances:

  code is as follows copy code

 

<script type=" Text/javascript "src="/demo/jquery/ Jquery-1.7.2.min.js
></script>
<script type= "Text/javascript"
$ (document). Ready ( function () {
   $ ("#button"). Click (function () {
     alert ($ ("#myForm"). Serialize ());
  });
});
</script>
<form id= "MyForm"
    name <input value= "liming" name= "name"/>& Lt;br/>
    position <input value= "CEO" name= "position"/><br/>
   & nbsp;    <input id= "button" value= "serialized form" type= "button"/>
</form>
</form>

Name
Position

Tips

Personally more recommended to do jquery Ajax Oh, this is not compatible and have a variety of function support such as load,post,get,getjson,getscript can be quickly implemented AJAX load.

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.