Home >Web Front-end >JS Tutorial >Sharing common methods of encapsulating Ajax with JQuery
easy-ajax was born to control the front-end random writing of Ajax methods. Next, this article will introduce to you the common methods of encapsulating Ajax with JQuery. Friends who need it can refer to it. I hope it can help you.
why easy-ajax
easy-ajax was born to control the front-end random writing of Ajax methods. Based on jquery to encapsulate some common methods, we plan to add front-end data caching function in the future.
The usage method is as follows:
<script src="easy.ajax.js"></script>
API:
config configuration item:
config{ jqueryAjax默认配置外增加如下配置: mustCallback:强制回调(很多情况请求失败后直接在Ajax中提示后端返回的错误信息就结束了,不会进入回调函数,但很难避免失败也需要处理逻辑的情况,所以增加了此项配置,无论成功失败皆执行配置中的callback,前提是有回掉函数。) }
Use the from form method to handle parameter request background (used when the interface uses @RequestParam)
EasyAjax.post_form_json
Use ordinary Method request (used when the interface uses @RequestBody)
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) { //上传成功 } });
File upload method 2:
Configure the elem method directly, the usage is as follows:
EasyAjax.ajax_Upload_File_Elem({ url:"URL" , elem: "#icon" },function (res) { if (res.success) { //上传成功 } });
Related recommendations:
JavaScript encapsulates ajax's js
JavaScript encapsulates the data code passed by Ajax_javascript skills
Native JS encapsulated Ajax plug-in (same domain, jsonp cross-domain)_javascript skills
The above is the detailed content of Sharing common methods of encapsulating Ajax with JQuery. For more information, please follow other related articles on the PHP Chinese website!