使用Ajax的Javascript在Mozilla中运行良好,但在Chrome中运行不正常

时间:2016-09-19 10:38:55

标签: javascript ajax

我正在使用XMLHTTPREQUEST同步,当请求得到满足时,我隐藏了页面上的所有内容并显示了进度消息。这在Mozilla中运行良好,但在Chrome中不起作用。

Mozilla firefox: before submit

after submit

对于chrome:

[提交后] [3]

这是我的javascript代码:

if (valid) {

    document.getElementById('contentwrapper').style.display="none";
    document.getElementById('contentwrapper').style.visibility="hidden";

    var cboxes = document.getElementsByName('items[]');
    var len = cboxes.length;
    var res1;
    var res2;
    var res3;
    var res4;
    var arrResult;

    for (var i = 0; cboxes[i]; ++i) {
        if (cboxes[i].checked) {
            checkedValue = cboxes[i].value;
            arrResult = checkedValue.split(',');
            mobileNo=arrResult[0];
            if(arrResult[0].trim().startsWith('0')){
                res1 = arrResult[0].substring(1,arrResult[0].length);
            }else{
                res1 = arrResult[0];
            }
            res2 = arrResult[1];
            res3 = arrResult[2];
            res4 = arrResult[3];
            requestSender(1, res1, res2, res3, res4);
        }
    }
function requestSender(code, mobile, operator, productCode, amount) {

var http = getHTTPObject();
var enterKeyHandler = true;
var reqNum = 0;
var seqNum = 0;
var dataCount = 0;
var Values = "";
var code;
var comments = "";
reqCode = code;
if (code == 1) {

    var agentcode = '<%=payeeaccountno%>';
    var pin = document.getElementById("mpin").value;
    source = agentcode; // source and agent code both are same.
    operator = operator;
    destination = mobile;
    productCode = productCode;
    if (destination.charAt(0) == '0') {
        destination = destination.replace(/^0+/, "");
    }
    var amt = amount;
    var amtPrd = new Array();
    amtPrd = amt.split("@");
    if (amtPrd.length > 0)
    {
        amount = amtPrd[0];
        //   productCode = amtPrd[1];
    }
    else
    {
        amount = 0;
    }
    clienttype = "SELFCARE";
    vendorcode = '<%=vendorcode%>';
    dataCount = '<%=cryptHandler.encrypt("8",session.getId()+session.getId())%>';
    reqNum = '<%=cryptHandler.encrypt("1",session.getId()+session.getId())%>';
    seqNum = '<%=cryptHandler.encrypt("2",session.getId()+session.getId())%>';
    Values = "&var1=" + escape(agentcode) + "&var2=" + escape(pin) + "&var3=" + escape(amount) + "&var4=" + escape(mobile) + "&var5=" + escape(productCode) + "&var6=" + escape(clienttype) + "&var7=" + escape(operator) + "&var8=" + escape(vendorcode);
}
var varReq = "../Comman/requestForwarder.jsp?page=" + escape("<%=reqPageName%>") + "&reqNum=" + escape(reqNum) + "&seqNum=" + escape(seqNum) + "&dataCount=" + escape(dataCount) + Values;
varReq = varReq.replace(/\+/g, "%2B");
http.open("GET", varReq, false);
httpBuffer = http;
reqCodeBuffer = reqCode;
http.onreadystatechange = responseHandler;
http.send(null);
}

1 个答案:

答案 0 :(得分:0)

你应该发布一些代码或者至少在开发者控制台中打印消息(ctr + shift + J)

但是,您的问题的根源可能是您尝试在“主”执行线程上使用同步xmlhttpRequest。

这是一个不好的做法(因为它阻止所有代码运行,直到服务器“回答”你的请求)并且浏览器开始逐步淘汰它。因此,如果您使用较新版本的Chrome,则可能无法运行代码并抱怨同步请求。

使用Web Worker(js版本的线程)来运行同步XMLHTTPRequest,或者更好的是,如果可能的话,使用异步版本。

进一步阅读:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests