zoukankan      html  css  js  c++  java
  • jquery ajax 设置请求头header 参数

    // 先假装有loading组件(util.hideLoading, util.showLoading),util.toast组件, time:表示loading时间


     

    let ajax = (url, data = {}, params = {

    showLoading: true,
    showError: false,
    method: 'GET', // 默认是GET,POST
    dataType: 'json',
    loadingTxt: '加载中...',
    headers: {}
    }) => {
    return new Promise((resolve, reject) => {
    let { method, showLoading = true, showError, loadingTxt, dataType, headers = {} } = params

    if (headers['Content-Type'].indexOf('application/json') > -1) {
    if (method.toUpperCase() == 'POST') {
    data = JSON.stringify(data)
    }
    }
    showLoading && util.showLoading(loadingTxt, time)
    let starttime = Number(new Date());
    $.ajax({
    url,
    type: method.toUpperCase(),
    dataType,
    xhrFields: {
    withCredentials: true
    },
    crossDomain: true,
    data,
    headers: {
    ...headers
    }
    })
    .done(res => {
    util.hideLoading();
    let {
    code,
    } = res;
    if (res.returnCode == 200 || res.isSuccess || res.successful) {
    resolve(res.result || res.value || res.data); // 看后端怎么包装返回数据
    } else {
    showError && util.toast(res.message || res.returnMsg);
    reject(res);
    }
    })
    .fail(res => {
    showLoading && util.hideLoading();
    reject(res && res.data);
    })
    .always(res => {
    console.log('-'.repeat(51))
    console.log("request data:", { url, data });
    console.log("request params:", params);
    console.log('response data', res);
    console.log('excute time: ', (Number(new Date()) - starttime) + 'ms');
    console.log('-'.repeat(51))
    })

    })
    }

  • 相关阅读:
    JavaScript window对象属性和方法
    bzoj1878 [SDOI2009]HH的项链
    bzoj3289 Mato的文件管理
    bzoj2038 [2009国家集训队]小Z的袜子(hose)
    bzoj2333 [SCOI2011]棘手的操作
    bzoj2809 [Apio2012]dispatching
    hdu1512 Monkey King
    免费航班
    bzoj4538 [Hnoi2016]网络
    bzoj3207 花神的嘲讽计划Ⅰ
  • 原文地址:https://www.cnblogs.com/xuLessReigns/p/11211975.html
Copyright © 2011-2022 走看看