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.