我知道执行AJAX调用的巨大优势在于,在某个元素准备就绪之前,页面的其余部分可以加载并为用户做好准备.但我有一些特殊的业务要求.
首先,由于体系结构,我必须使用AJAX.其次,要求是我不能创建某个部分的延迟加载的外观.所以,我需要在返回jQuery AJAX调用之前不呈现页面.
到目前为止,我唯一的尝试就是简单地从jQuery document.ready函数中取出AJAX调用,认为它会立即触发并可能延迟页面.这没用.
Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user’s experience. (This is a long process that takes many years.) Developers must not pass false for the async argument when the JavaScript global environment is a document environment. User agents are strongly encouraged to warn about such usage in developer tools and may experiment with throwing an InvalidAccessError exception when it occurs.
jQuery.ajax()运动异步参数.设置为false时,您的AJAX请求将变为同步/阻止.
至于页面加载:不要挂钩任何“就绪”事件.只需在加载jQuery库后直接调用:
$('body').css('display','none');
然后运行你的(现在同步的)AJAX请求.
关于AJAX完成/成功,Do
$('body').css('display','block');
干杯.