ajax 初始化请求前携带参数

时间:2021-01-25 19:31:23


  $( function  () {
     function SetAjax(wxOpenId, departCode) {
        $.ajaxSetup({
            xhrFields: {
                withCredentials:  true
            },
            beforeSend:  function (o) {
                 if (wxOpenId && departCode) {
                    o.setRequestHeader('wxOpenId', " " + wxOpenId);
                    o.setRequestHeader('DepartCode', " " + departCode);
                }
            }
        });
    }

     var wxOpenId = App.GetQueryStringByName("wxOpenId");
     var DepartCode = App.GetQueryStringByName("DepartCode");

     if (!wxOpenId) {
        wxOpenId = App.GetCookie("wxOpenId");
    }  else {
        App.SetCookie("wxOpenId", wxOpenId, 365);
    }

     if (!DepartCode) {
        DepartCode = App.GetCookie("DepartCode");
    }  else {
        App.SetCookie("DepartCode", DepartCode, 365);
    }

     if (wxOpenId && departCode){
        SetAjax(wxOpenId, departCode);
    }
});