The difference between $.ajax (), $.get (), $.post (), and some parameters note the rules

Source: Internet
Author: User
Tags string format

Comparison of $.ajax () method and $.get (), $.post () method

The $.ajax () method is the most complete way to do all of the Ajax requests (including get types and post types)

$.get () and $.post () are simple versions that can be used to complete a single, simple function, in order to make the code look less cumbersome, concise, and save a lot of unnecessary functionality.


$.get ()
Used to fetch data from the destination address, but parameters cannot be passed (data can now be passed)

-----------------------------------------------------------------


Syntax format: $.get (Url,callback);

The URL required parameter specifies the URL that you want to request.

Callback optional parameter is the name of the function executed after the request succeeds.

eg

$ ("button"). Click (function () {
$.get ("demo_test.asp", function (data,status) {
Alert ("Data:" + Data + "\nstatus:" + status);
});
});

-----------------------------------------------------------------


Latest Syntax format: $.get (Url,data,function (DATA,STATUS,XHR), DataType)

URL required. Specify the URL that you want to request.

Data is optional. Specifies the data that is sent to the server along with the request.

function (DATA,STATUS,XHR) is optional. Specifies the function to run when the request succeeds.
Additional parameters:
Data-contains results from the request
Status-Contains the requested State ("Success", "Notmodified", "Error", "Timeout", "ParserError")
XHR-Contains XMLHttpRequest objects

DataType is optional. Specifies the data type of the expected server response.
By default, JQuery will be smart to judge.
Possible types:
"XML"-an XML document
"HTML"-HTML as plain text
"Text"-Plain text string
"Script"-Runs the response in JavaScript and returns it as plain text
"JSON"-runs the response in JSON and returns it as a JavaScript object
"Jsonp"-load a JSON block with Jsonp and add a "? callback=?" To the URL to specify the callback



-----------------------------------------------------------------



$.post ()
To get the data from the destination address, you can pass the parameters to complete the conditional filtering


Syntax format: $ (selector). Post (Url,data,function (DATA,STATUS,XHR), DataType)


URL required. Specify the URL that you want to request.

Data is optional. Specifies the data that is sent to the server along with the request.

function (DATA,STATUS,XHR) is optional. Specifies the function to run when the request succeeds.
Additional parameters:
Data-contains results from the request
Status-Contains the requested State ("Success", "Notmodified", "Error", "Timeout", "ParserError")
XHR-Contains XMLHttpRequest objects

DataType is optional. Specifies the data type of the expected server response.
By default, JQuery will be smart to judge.
Possible types:
"XML"-an XML document
"HTML"-HTML as plain text
"Text"-Plain text string
"Script"-Runs the response in JavaScript and returns it as plain text
"JSON"-runs the response in JSON and returns it as a JavaScript object
"Jsonp"-load a JSON block with Jsonp and add a "? callback=?" To the URL to specify the callback



-----------------------------------------------------------------

So, for now, what's the difference between the $get () and the $post () method, which is exactly the same function?
The other difference is that they

The GET method may return cached data.
The POST method does not cache data

This is the biggest difference between them.
So you can choose to use $get () or $post () depending on your needs.


Detailed description
The $get () or $post () functions are abbreviated Ajax functions in exactly the same format, equivalent to:

$.ajax ({
Type: ' Post/get ',
Url:url,
Data:data,
Success:success,
Datatype:datatype
//...
});

-----------------------------------------------------------------


Also note that the type of data passed must be the object type (format of the key-value pair: JSON) or String type

(requires data to be sent to the server as an object or a string of type parameters.) )

If it is not already a string, it is automatically converted to a string format.
The GET request will be appended to the URL. To prevent this automatic conversion, you can view the ProcessData option.
The object must be in key/value format, for example {foo1: "Bar1", Foo2: "Bar2"} to &foo1=bar1&foo2=bar2.
In the case of arrays, jquery automatically corresponds to the same name for different values.
For example {foo:["Bar1", "Bar2"]} is converted to &FOO=BAR1&FOO=BAR2.

If you want to pass an array to the server, you have to turn the array into an object type, and then the server gets the re-processing

$.ajax ({
Type: ' POST ',
Url:url,
Data: [All-in-a-way], so the assignment will error
Data: {' arrayobj[] ': [1,2,3,4]} This assignment is OK
Success:success,
Datatype:datatype
})

Resources:

AJAX get () and post () methods

JQuery AJAX method

$.ajax () method detailed

The difference between $.ajax (), $.get (), $.post (), and some parameters note the rules

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.