Ajax
XMLHttpRequest is the basis for Ajax implementation
It is an object and has several methods and attributes to remember:
XMLHttpRequest Method
Open (method, URL, async, username, password)
Method: post or get
URL: request address
Async: Boolean. The default value is true. Is it asynchronous?
There is basically no meaning behind it...
Send (content)
After XMLHttpRequest calls the open method, there is no actual request event.
The request is actually sent only when the send command is executed.
When the open method is set to get, the open parameter is null. If it is post, the content must be a string in the format of 'a = 1 & B = 2.
XMLHttpRequest attributes
Onreadystatechange: The Program executed when the Request status changes.
Readystate: There are four status values: 0, 1, 2, 3, and 4. 4 indicates that the request has been completed.
Responsexml: XML returned by the server
Responsetext: string returned by the server
Status: 200,404 or other status codes
xmlHttpRequesst.onreadystatechange = function(){ if(xmlHttpRequesst == 4){ if (xmlHttpRequesst.status==200){ document.getElementById("some").innerHTML = xmlHttpRequesst.responseText; } }}xmlHttpRequest.open(‘GET‘, ‘/some/resource/url‘);xmlHttpRequest.send(null);
In fact, each well-known browser has its own XMLHTTPRequest object, but the storage location is different:
if (window.XMLHttpRequest) { xmlHttpRequest = new XMLHttpRequest();} else if (window.ActiveXObjext) { xmlHttpRequest = new ActiveXObjext("Msxml2.XMLHTTP");} else { // rubbish browser}
Jquery Ajax way
Load (URL, parameters, callback)
The caller of this method is a package set that loads the content returned by the server to the specified package set element...
URL: the address of the resource requested by the server
Parameter: Request Parameter. If this parameter is specified, it is the POST method. If this parameter is not specified, it is a GET request.
Callback: the callback function called after the server resources are loaded to the page. The parameter of this function is the corresponding text, status, and xhr instance.
Serialize ()
Serializearray ()
$(function(){ $(‘someSelector‘).load( ‘url‘, { ‘name‘ : ‘voctrals‘, ‘age‘ : ‘11‘ }, function(){ alert(‘finished~‘); } );});
If the content retrieved from the server is not directly displayed in the browser, you can use other methods. This is the case described below.
The caller of these methods is jquery, not a package set.
Common get
$. Get (URL, parameters, callback)
Retrieve required items from the server using the get Method
URL: server address
Parameters: String, 'a = 1 & B = 2'
Object, {A: 1, B: 2}
Callback: There are two parameters. The first is the content returned by the server, and the second is the status code.
Getjson
It is similar to get, except that the server returns a JSON object. This method is more convenient.
Normal post
Basically the same as get
Complete Ajax
$. Ajax (options)
Options: Object
$.ajax( { url : ‘/voctrals‘, type : ‘POST‘, data : {name : ‘tla‘, age : ‘20‘}, dataType : ‘html‘, timeout : ‘3000‘, success : function(data, status){ }, error : function(){}, complete : function(){}, beforeSend : function(){}, async : false });
$. Ajaxsetup (properties)
You can set the default value for $. Ajax calls.
$.ajaxSetup( { type : ‘POST‘, dataType : ‘html‘, timeout : ‘3000‘, error : function(){}, async : false });