zoukankan      html  css  js  c++  java
  • 封装ajax

    'use strict';
    function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
    var cookie = jQuery.trim(cookies[i]);
    // Does this cookie string begin with the name we want?
    if (cookie.substring(0, name.length + 1) === (name + '=')) {
    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
    break;
    }
    }
    }
    return cookieValue;
    }

    // 自己封装的ajax请求
    // 'beforeSend':function(xhr,settings) {
    // var csrftoken = getCookie('csrftoken');
    // //2.在header当中设置csrf_token的值
    // xhr.setRequestHeader('X-CSRFToken',csrftoken);
    // }

    // function A(args){
    // console.log(args);
    // }

    // function B(args){
    // args['username'] = 'xt';
    // A(args);
    // }

    // A({'username':'xt'});
    // A({'username':'xt'});
    // A({'username':'xt'});
    // A({'username':'xt'});
    // B();

    var myajax = {
    'get':function(args) {
    args['method'] = 'get';
    this.ajax(args);
    },
    'post':function(args) {
    args['method'] = 'post';
    this.ajax(args);
    },
    'ajax':function(args) {
    // 设置csrftoken
    this._ajaxSetup();
    $.ajax(args);
    },
    '_ajaxSetup': function() {
    $.ajaxSetup({
    'beforeSend':function(xhr,settings) {
    var csrftoken = getCookie('csrftoken');
    //2.在header当中设置csrf_token的值
    xhr.setRequestHeader('X-CSRFToken',csrftoken);
    }
    });
    }
    }

  • 相关阅读:
    Docker学习笔记
    Docker学习笔记
    Docker学习笔记
    Docker学习笔记
    Docker学习笔记
    第二类斯特灵数学习笔记
    浅谈"n个球"和"m个盒子"之间的乱伦关系
    cf932E. Team Work(第二类斯特灵数 组合数)
    [vijos]lxhgww的奇思妙想(长链剖分)
    BZOJ2054: 疯狂的馒头(并查集)
  • 原文地址:https://www.cnblogs.com/lajiao/p/9074734.html
Copyright © 2011-2022 走看看