异步ajax调用阻塞UI

时间:2013-11-06 10:13:07

标签: javascript ajax asynchronous

呈现JSP后,我正在尝试通过ajax进行异步调用,以在页面上加载其他信息。

我期望在不妨碍UI滚动条导航的情况下优雅地执行ajax加载。但是调用会阻止UI,直到onLoad完成。

在其他时候,即使在鼠标单击ajax调用时,此服务也会阻止UI(光标保持为pointer类型,直到加载数据为止。)

在这两种情况下,我都是通过javascript构建DOM(比如为div或table创建innerHTMl)。是因为这个吗?或者是其他东西?我正在附上我的ajax请求代码。

感谢您的帮助。 (抱歉,我尝试格式化代码,但我无法在此处得到它)

function requestService(theService, theParamObj, isSyncCall) {
    var ajaxRequest = getAjaxRequest();  
    var params = "data="; 
    if(theParamObj != null)
        params += encodeURIComponent(JSON.stringify(theParamObj));

    ajaxRequest.onreadystatechange = function() {
        if (ajaxRequest.readyState == 1) {
            showLoadingBox();
        }
        if (ajaxRequest.readyState == 4) {          
            handleResponse(ajaxRequest.responseText, theService, theParamObj);
            hideLoadingBox();
        }
    };

    var queryString = "?timestamp=" + new Date().getMilliseconds() + "&theService=" + theService;
    if(isSyncCall == null)
        isSyncCall = false;
    ajaxRequest.open("POST", g_Service + queryString, isSyncCall);
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.send(params);
}

更新: onLoad调用此服务

function loadAdInfo(){
   var theParamObj = {"REQUEST_URI" : window.location.href};
   requestService('getAdInfo', theParamObj, false);
}

1 个答案:

答案 0 :(得分:3)

XMLHTTPObject open方法的定义如下:

open(method,url,async)  Specifies the type of request, the URL, and if the request should be handled asynchronously or not.

method: the type of request: GET or POST
url: the location of the file on the server
async: true (asynchronous) or false (synchronous)

你传递的是假的。所以你称之为同步