Before sending an Ajax request from a client page to the server, you must first create the XMLHttpRequest object in js. After the object is created, use OPEN (& amp; #39; GET/POST & amp; #39;, URL, synchronous/asynchronous) set the submission method, URL address, synchronous or asynchronous mode. Then use send (data)... S
Before sending an Ajax request from a client page to the server, you must first create the XMLHttpRequest object in js. After the object is created, use OPEN ('get/Post', URL, synchronous/asynchronous) set the submission method and URL address, synchronous or asynchronous. Then, use send (data) to send data to the server, and use onreadystatechange to bind the callback function. If you submit data using the GET method, splice the data directly in the URL address. If you use the POST method, you must first set the request header setRequestHeader ("Content-Type ", "application/x-www-form-urlencodeed"), and then use the send () method to send data. In the callback function, you also need to determine whether the current status has been completed (readyState = 4), whether the server has returned (status = 200), and then perform corresponding processing. The whole process is very troublesome, And the XMLHttpRequest created will be different based on the browser, which is very troublesome.
It is very convenient to use the ajax (option) That jQuery provides for us. Just do this:
$. Ajax ({
Type: "POST ",
Url: "RegService. do ",
Data: "name = John & location = Boston ",
DataType: 'json ',
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});
Type specifies whether to submit data through post or get. The url is the URL address of the data submitted to the server, and the data is the data submitted to the server, if the get method is used, you can also splice the data in the URL address. dataType indicates the organization of the data returned by the server, and success indicates the callback function when the data is successfully returned. You can also use jQuery. get (url, [data], [callback]). This is a simple GET request function to replace complex $. ajax. You can call the callback function when the request is successful. JQuery. getJSON (url, [data], [callback]) loads JSON data through http get requests. JQuery. post (url, [data], [callback]) is a simple POST Request function to replace complex $. ajax. You can call the callback function when the request is successful. JQuery. getScript (url, [callback]) loads and executes a JavaScript file through an http get request. For example: $. getScript ("test. js", function (){
Alert ("Script loaded and executed .");
});