Home >Web Front-end >JS Tutorial >Detailed explanation of Ajax get, post and other methods in jQuery_jquery
load() method is usually used to obtain static data files from the web server, but this does not reflect the full value of ajax.
In the project, if you need to pass some parameters to the page on the server, you can use the $.get() or $.post() method (or the $.ajax() method)
$.get() method Uses GET method to make asynchronous requests. The structure is: $.get(url [, data] [, callback] [, type])
$.get() method parameters are explained as follows:
Parameter name | Type | Description |
url | String | The URL address of the requested HTML page |
data(optional) | Object | The key/value data sent to the server will be appended to the request URL as QueryString |
callback(optional) | Function | The callback function is called when loading is successful (this method is only called when the return status of Response is success) and the request result and status are automatically passed to this method |
type(optional) | String | The format of content returned by the server, including xml, html, script, json, text and _default |
$.post() method.
The structure and usage of the $.post() and $.get() methods are the same, but there are still the following differences between them:
The GET request will pass the parameters after the URL, while the POST request will be sent to the web server as the entity content of the HTTP message.
The GET method has a size limit on the data transmitted (usually no more than 2KB), while the amount of data transmitted using the POST method is much larger than the GET method (theoretically not limited)
The data requested by GET method will be cached by the browser, so others can read the data from the browser's history, such as account number and password. In some cases, the GET method will cause serious security problems, while the POST method can relatively avoid these problems
The data transferred by GET method and POST method are obtained differently on the server side.
$.getScript(): jQuery provides this method to load js files directly, which is as simple and convenient as loading an HTML fragment, and does not require processing of JavaScript files, JavaScript files will be executed automatically.
The jQuery code is as follows:
Like other ajax methods, the $.getScript() method also has a callback function, which will run after the JavaScript file is loaded successfully.
For example: If you want to load the jQuery official color animation plug-in (jquery.color.js), and bind the color change animation to the element after success:
$.getJson(): This method is used to load JSON files, and its usage is the same as $.getScript().
test.json文件为:
Note:
jQuery will automatically replace the callback function in the URL, such as the last "?" in "url?callback=?", with the correct function name to execute the callback function.
JSONP (JSON with Padding) is an unofficial protocol that allows integrating Script tags on the server side and returning them to the client, achieving cross-domain access through JavaScript Callback. Since JSON is just plain text with a simple bracket structure, many channels can exchange JSON messages. Due to the restrictions of the same origin policy, developers cannot use XMLHttpRequest when communicating with external servers. JSONP is a method that can bypass the same origin policy, that is, by using a combination of JSON and