• 封装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);
    }
    });
    }
    }

  • 相关阅读:
    oracle sql日期比较:
    vs 2008 过期问题
    silverlight带有复选框的列
    SQL 把一张表虚拟成两张表
    timeupdown
    ChildWindow 父窗体交互
    Debian CentOS修改时区
    如何优雅地使用命令行设置windows文件关联
    sql复制表结构,复制表内容语句
    VC6.0 中 添加/取消 块注释的Macro代码
  • 原文地址:https://www.cnblogs.com/lajiao/p/9074734.html
Copyright © 2020-2023  润新知