JQuery encapsulates common Ajax methods (recommended) and jqueryajax
Why easy-ajax
Easy-ajax is generated to manage the frontend garbled Ajax method. Some common methods are encapsulated based on jquery, And the frontend data cache function is planned to be added later.
The usage is as follows:
<script src="easy.ajax.js"></script>
API:
Config configuration items:
In config {jqueryAjax, the following configuration is added by default: mustCallback: Forced callback (in many cases, after a request fails, an error message returned by the backend is returned directly in Ajax, which does not enter the callback function, however, it is difficult to avoid failure and the logic needs to be processed. Therefore, with this configuration added, the callback in the configuration is executed no matter the failure is successful, provided that a function is returned .)}
Use the from form method to process the parameter request background (used when the interface uses @ RequestParam)
EasyAjax.post_form_json
Common Request (used when @ RequestBody is used for the interface)
EasyAjax.post_json
File Upload Method 1:
Var file = $ (". xxx "). get (0 ). files [0]; var fileData = new FormData (); fileData. append ("file", file); EasyAjax. ajax_Upload_File ({url: "URL", data: fileData}, function (data) {if (data. success) {// upload successful }});
File Upload Method 2:
You can directly configure the elem method as follows:
EasyAjax. ajax_Upload_File_Elem ({url: "URL", elem: "# icon"}, function (res) {if (res. success) {// uploaded successfully }});
The above is the JQuery encapsulated Ajax common method (recommended) introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!