JQuery asynchronous request Ajax application $. get $. post $. getScript $. getJson method introduction

Source: Internet
Author: User
Tags getscript json

When jquery and ajax are used together, they should be $. get and $. post. The usage is simple: url + parameter + callback function + return value type.

Before that, summarize the differences between. load and $. get $. post:

. Load: local effect ,. the first half of the load will have an element selection, for example: $ ('# box '). load () is for the element id = box. It is more suitable for obtaining static files.

$. Get $. post: Global, which is controlled by the callback function. It is more suitable for dynamic interaction with servers.

See $. get

The code is as follows: Copy code

$ ('# Ajax'). click (function (){
$. Get ('test. php? Domain = aitiblog ', function (data) {// get request server data
Console. log (data );
}, 'Json'); // if the returned plain text is not written to the last parameter by default, it is OK. If you want to return a json result, assign the last parameter as json.
});


The following are very similar to post and get.

The code is as follows: Copy code


$ ('# Ajax'). click (function (){
$. Post ('test. Php', {domain: 'www .aitiblog.com '}, function (data) {// post request server data
Console. log (data );
}, 'Json'); // if the returned plain text is not written to the last parameter by default, it is OK. If you want to return a json result, assign the last parameter as json.
});

$. GetScript is a technical blind spot. It has never been used before. Make up for it here.

$ ('# Ajax'). click (function () {// load test. js from the server after the click event is triggered, saving resources
$. GetScript ('test. Js ');
});

Another getJson is not described here. It is similar to get, which is returned in json format by default.

Finally, I would like to summarize my experience: it is recommended that you use post to send data interactions for the following reasons:

1. It is convenient to view the post in the developer tool debugging url, and the parameter arrangement is regular and easy to view.

2. There is no size limit for post data sending, but get is up to 2 kB.

3. data sent by post, such as the url address, is more advantageous than get. If the get method is used to send a url, there will be an error and it is not beautiful. It is very effective to put the data in the http object by post without escaping.

Related Article

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.