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.