我的代码有问题。
我想对全局变量使用ajax响应,以便该变量可以在其他函数中使用。
但是我从控制台收到一条警告,即不建议使用Syncronous。
有运行它的替代方法吗?
session.js
let SESSION = {}
const sessionController = (() => {
const getSession = () => {
if (TOKEN) {
return $.ajax({
url: `${BASE_URL}int/setting/user_info`,
type: 'GET',
dataType: 'JSON',
async: false,
global: false,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(USERNAME + ":" + PASSWORD))
xhr.setRequestHeader("SCM-INT-KEY", TOKEN)
},
success: function (res) {
SESSION = res.data
},
error: function (err) {
window.location.replace(`${BASE_URL}private`)
}
});
} else {
window.location.replace(`${BASE_URL}private`)
}
}
return {
init: () => {
getSession()
}
}
})();
sessionController.init()
app.js
const renderApp = (() => {
const DOM = {
menu: '#menu',
content: '#content'
}
return {
renderNavigation: () => {
let { level } = SESSION
let menu = '';
switch (level) {
case 'Admin':
menu += `<h1> Navbar Admin </h1>`
break;
case 'Finance':
menu += `<h1> Navbar Finance </h1>`
break;
case 'Warehouse':
menu += `<h1> Navbar Warehouse </h1>`
break;
default:
break;
}
$(DOM.menu).html(menu);
},
renderPage: (page) => {
$(DOM.content).html(page);
}
}
})();
const appController = ((UI) => {
const getPage = path => {
let FULLPATH = `${BASE_URL}${path}`
let { level } = SESSION
if (path === 'dashboard') {
FULLPATH = `${path}/${level}`
}
$.get(FULLPATH, function (page) {
UI.renderPage(page)
})
}
const setRoute = () => {
if (!location.hash) {
location.hash = '#/dashboard';
} else {
let path = location.hash.substr(2);
getPage(path);
}
$(window).on('hashchange', function () {
let path = location.hash.substr(2);
getPage(path);
});
}
return {
init: () => {
UI.renderNavigation(),
setRoute()
}
}
})(renderApp);
$(document).ready(function () {
appController.init();
})
请帮助我。 给我一些例子来解决这个问题
谢谢