问题: 当项目启动登录后,google浏览器(F12)或fireFox等浏览器会出现如下警告:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. jquery-1.8.0.min.js:2
For more help, check https://xhr.spec.whatwg.org/.
send | @ | jquery-1.8.0.min.js:2 | |
ajax | @ | jquery-1.8.0.min.js:2 | |
(anonymous) | @ | notice.js?v=201502100941:26 | |
k | @ | jquery-1.8.0.min.js:2 | |
fireWith | @ | jquery-1.8.0.min.js:2 | |
ready | @ | jquery-1.8.0.min.js:2 | |
D | @ | jquery-1.8.0.min.js:2 |
原因:加载项目对应的*.js时,若主*.js中ajax使用async: false,而后面的*.js中使用async: true,就会造成ajax加载不同步,从而出现上面警告。
async: true,(默认是true),ajax请求是异步的;
async: false,
ajax请求是同步的;此时ajax请求将整个浏览器锁死,只有*.js对应的*.jsp页面执行结束后,才可以执行其它操作。
例如:notice.js
$(function() { $.ajax({ type: "POST", url: "querySysNoteByParam.do?rows=5", dataType: "json", async: false, success: function(data) { var list = $("#list-tpl").html(), item = $("#item").html(); var row = ""; var $tab = $(".note-list"); $.each(buildData(data), function(i, n) { var temp = ""; $.each(n.data, function(a, b) { temp += item.replace(/\$itemTitle/g, StringUtils._encodeHTML((StringUtils.getStrLength(b.noteTitle) > 16 ? (b.noteTitle.substring(0, 16) + "...") : b.noteTitle))) .replace(/\$Titlelong/g, StringUtils._encodeHTML(b.noteTitle)) .replace(/\$id/g, b.id) .replace(/\$dateTime/, b.publishDate.substring(0, 10)); }); row += list.replace(/\$title/g, n.title).replace(/\$list/, temp).replace(/\$noteType/g, n.noteType); }) $tab.html(row); } }); });
修改成:
async: true, 这样就不会出现那个警告了。