Jquery.get () Method:
$.get (Url,data,success (RESPONSE,STATUS,XHR), DataType)
This function is shorthand for the Ajax function, which is equivalent to:
[JavaScript]View PlainCopy
- $.ajax ({
- Url:url,
- Data:data,
- Success:success,
- Datatype:datatype
- });
The return data passed to the success callback function differs depending on the MIME type of the response, which can be an XML root element, a text string, a JavaScript file, or a JSON object. You can also pass the text state of the response to the success callback function.
Parameters |
Describe |
Url |
Necessary. Specifies which URL to send the request to. |
Data |
Optional. Specifies the data that is sent to the server along with the request. |
Success (RESPONSE,STATUS,XHR) |
Optional. Specifies the function to run when the request succeeds. Additional parameters: Response-Contains the result data from the request Status-Contains the state of the request XHR-Contains XMLHttpRequest objects |
DataType |
Optional. Specifies the data type of the expected server response. By default, JQuery will intelligently judge. Possible types: "XML" "HTML" "Text" "Script" "JSON" "Jsonp" |
Use the AJAX GET request to change the text of the DIV element:
[JavaScript]View PlainCopy
- $ ("button"). Click (function () {
- $.get ("demo_ajax_load.txt", function (result) {
- $ ("div"). html (result);
- });
- });
Jquery.post () Method:
$.post (url,data,success (data, Textstatus, JQXHR), DataType)
This function is shorthand for the Ajax function, which is equivalent to:
[JavaScript]View PlainCopy
- $.ajax ({
- Type: ' POST ',
- Url:url,
- Data:data,
- Success:success,
- Datatype:datatype
- });
The return data passed to the success callback function differs depending on the MIME type of the response, which can be an XML root element, a text string, a JavaScript file, or a JSON object. You can also pass the text state of the response to the success callback function.
Parameters |
Describe |
Url |
Necessary. Specifies which URL to send the request to. |
Data |
Optional. The map or string value. Specifies the data that is sent to the server along with the request. |
Success (data, Textstatus, JQXHR) |
Optional. The callback function to execute when the request succeeds. |
DataType |
Optional. Specifies the data type of the expected server response. The default is to perform smart judgments (XML, JSON, script, or HTML). |
Use the AJAX GET request to change the text of the DIV element:
[JavaScript]View PlainCopy
- $ ("input"). KeyUp (function () {
- txt=$ ("Input"). Val ();
- $.post ("demo_ajax_gethint.asp", {suggest:txt},function (result) {
- $ ("span"). HTML (result);
- });
- });
Instance:
A simple POST request function to replace the complex $.ajax. The callback function can be called when the request succeeds. If you need to execute the function on error, use $.ajax.
Example code:
Ajax.aspx:
[JavaScript]View PlainCopy
- Response.ContentType = "Application/json"; Response.Write ("{result: '" + request["Name"] + "Hello! (The message comes from the server) '} ');
JQuery Code:
[JavaScript]View PlainCopy
- $.post ("ajax.aspx", {Action: "Post", Name: "Lulu"},
- function (data, textstatus) {
- //data can be xmldoc, jsonobj, HTML, text, and so on.
- //this;
- //The option configuration information for this AJAX request, please refer to the Jquery.get () mentioned in this
- alert (Data.result);
- }, "JSON");
Click Submit:
The format of the request is "JSON":
$.ajax () This is the underlying AJAX implementation of jquery. Easy to use high-level implementation see $.get, $.post and so on.
Here are a few Ajax event parameters: Beforesend, Success, complete, error. We can define these events to handle each of our AJAX requests well.
[JavaScript]View PlainCopy
- $.ajax ({
- URL: ' stat.php ',
- Type: ' POST ',
- Data:{name:"Keyun"},
- DataType: ' html ',
- timeout:1000,
- Error: function () {alert (' Error loading PHP document ');},
- Success: function (Result) {alert (result);}
- });
The difference between $.get () and $.post ():
The computer network provides a simple explanation of the post and get methods in the HTTP protocol: the HTTP protocol defines two kinds of communication messages: the request message and the response message. For a request message, its common format consists of three main parts: the request line, the header line, and the entity body. Typically the request line is in the format: Method field, URL field, HTTP protocol version field, where the Method field includes get, post method, and so on.
When the Method field is a get method, the content body is empty, but the entity body is used instead of the Post method.
HTTP clients typically use the Post method when a user submits a form, where the entity body contains the user's input values in the form. Of course, the Get method can also submit data in the form, which is done by passing the user's input values in the form to the correct URL. This is what we usually see as the http://ei.hust.edu.cn?username= "Libis" &age=12.
As you can see, there is a big difference in the way that forms are submitted:
The 1.Post method populates the form data with the entity body part of the HTTP request message, which is not visible to the consumer. The Get method is a URL field that delivers form data directly to the request line to commit the form data. This also determines that the use of post is relatively safe, but with the get method of security is very low.
In addition, another important difference between the Get method and the Post method is:
The 2.GET method transmits a small amount of data, typically only 2k. And the Post method transmits the large amount of data, theoretically there is no upper limit.
$.ajax () Some places to be aware of:
There are three main ways of 1.data, HTML splicing, JSON array, form form is serialized by Serialize (), and no intelligent judgment is specified by datatype.
2.$.ajax only submits form in text mode, if the asynchronous commit contains <file> uploads are not passed over, you need to use Jquery.form.js $.ajaxsubmit
$.ajax () Practical application examples
[JavaScript]View PlainCopy
- 1.$.ajax asynchronous requests with JSON data
- var aj = $.ajax ({
- URL:' productmanager_reverseupdate ',//Jump to action
- data:{
- Selrollback:selrollback,
- Seloperatorscode:seloperatorscode,
- Provincecode:provincecode,
- Pass2:pass2
- },
- Type:' post ',
- Cache:false,
- DataType:' json ',
- Success:function (data) {
- if (data.msg = ="true") {
- //View ("Modified successfully!") ");
- Alert ("modified successfully! ");
- Window.location.reload ();
- }else{
- View (DATA.MSG);
- }
- },
- Error: function () {
- //view ("Exception! ");
- Alert ("Exception! ");
- }
- });
- 2.$.ajax asynchronous requests that serialize the contents of a table to a string
- function Notips () {
- var Formparam = $ ("#form1"). Serialize (); Serialize table contents as strings
- $.ajax ({
- Type:' post ',
- URL:' notice_notipsnotice ',
- Data:formparam,
- Cache:false,
- DataType:' json ',
- Success:function (data) {
- }
- });
- }
- 3.$.ajax asynchronous requests for stitching URLs
- var Yz=$.ajax ({
- Type:' post ',
- URL:' validatepwd2_checkpwd2?password2= ' +password2,
- data:{},
- Cache:false,
- DataType:' json ',
- Success:function (data) {
- if (data.msg = ="false") //The server returns false to change the value of ValidatePassword2 to Pwd2error, which is asynchronous and takes into account the return time
- {
- Textpassword2.html ("<font color= ' red ' > Business password is incorrect! </font> ");
- $ ("#validatePassword2"). Val ("pwd2error");
- CheckPassword2 = false;
- return;
- }
- },
- Error:function () {}
- });
- 4.$.ajax asynchronous request to splice data
- $.ajax ({
- URL:' <%=request.getcontextpath ()%>/kc/kc_checkmernameunique.action ',
- Type:' post ',
- Data:' mername= ' +values,
- Async: false, //default is True async
- Error:function () {
- Alert (' error ');
- },
- Success:function (data) {
- $ ("#" +divs). HTML (data);
- }
- });
jquery in $.get (), $.post (), and $.ajax ()