[关闭]
@Wangww0925 2019-08-07T07:58:34.000000Z 字数 755 阅读 211

jQuery ajax && ajax封装

jQuery


属性

  1. url 请求路径
  2. type 请求方式 get | post
  3. async 是否同步 true 默认,异步 | false 同步
  4. contentType "application/json;charset=utf-8"
  5. data 请求参数
  6. success 请求成功的回调
  7. error 请求失败的回调

例子

  1. $.ajax({
  2. url: "路径",
  3. type: "post",
  4. contentType: "application/json;charset=utf-8",
  5. data: "",
  6. success: function(data){
  7. console.log(data)
  8. },
  9. error: function(res){
  10. console.log(res)
  11. }
  12. })

ajax封装

  1. /**
  2. * jQuery ajax封装
  3. * 调用
  4. getAjaxOptions({
  5. async: false, // 同步
  6. data: "",
  7. success: function (result){
  8. console.log(result)
  9. }
  10. })
  11. */
  12. function getAjaxOptions(options){
  13. var opts = {
  14. contentType: "application/json;charset=utf-8",
  15. type: "POST",
  16. async: true // true 异步(默认) / false
  17. }
  18. $.extend(opts, options);
  19. opts.success = function(data) {
  20. opts.success ? opts.success(data) : ""
  21. };
  22. opts.error = function(res) {
  23. opts.err ? opts.err(res) : ""
  24. };
  25. $.ajax(opts);
  26. }

作者 wendy
2019 年 6月 5日

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注