ajax中的async設置問題


問題: 當項目啟動登錄后,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中,在進行ajax操作時,async設置有問題,如下:
原因:加載項目對應的*.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, 這樣就不會出現那個警告了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM