带有加载通知的跨浏览器 xmlhttprequest 响应

发布于 2024-11-18 20:06:35 字数 2483 浏览 1 评论 0原文

我有跨浏览器实用程序函数,用于设置正确的 XMLHttpRequest 对象,该对象是我从该站点复制的。其次,我希望有一个适当的函数来返回文本或加载加载数据的通知。如果加载数据需要很长时间,最好有错误通知。谢谢。

if (!AJAX) var AJAX = {};
else if (AJAX && typeof(AJAX) != "object")
throw new Error("AJAX is not an Object type");

JSAJAX = {
NAME: "AJAX",
VERSION: 1.0,

initAJAX: function(){
    var objxml = null;
    var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];            

    try{
        objxml = new XMLHttpRequest();
    }
    catch(e){                
        for (var i = 0; i < ProgID.length; i++){
            try{
                objxml = new ActiveXObject(ProgID[i]);
            }
            catch(e){                        
                continue;
            }
        }
    }

    return objxml;            
},

getAJAXResponseText: function(xhr){
    var outObj = {};

    outObj.outMsg = "";
    outObj.loadingFlag = false;
    outObj.errorFlag = false;

    if (xhr.readyState == 4){

        if (xhr.status == 200){
            outObj.outMsg = xhr.responseText;
            outObj.loadingFlag = false;
            outObj.errorFlag = false;
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;
        }

    }else{

        if (xhr.status == 200){
            outObj.loadingFlag = true;
            outObj.errorFlag = false;               
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;                
        }
    }

    return outObj;
}
}

这段代码在这里:

window.onload = makeRequest;
var xhr = false;
var currMsg;

function makeRequest() {
currMsg = document.getElementById("updateArea").innerHTML;

xhr = AJAX.initAJAX();

if (xhr) {
    xhr.onreadystatechange = showState;
    xhr.open("GET", "colors.xml", true);
    xhr.send(null);
}
else {
    document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}

function showState() {
var retObj = AJAX.getAJAXResponseText(xhr);

if (retObj.loadingFlag) { // Missing time expiriration of loading
    document.getElementById("updateArea").innerHTML = currMsg + "<h2>Loading...</h2>";
}else{
    document.getElementById("updateArea").innerHTML = currMsg + "<p>" + retObj.outMsg + "</p>";
}
}

I have cross browser utility function for setting up a proper XMLHttpRequest object, that I had copied from this site. Secondly, I would like to have a proper function for returning text or loading notification of loading data. If loading data takes to much time, it would be good to have an error notification. Thanks.

if (!AJAX) var AJAX = {};
else if (AJAX && typeof(AJAX) != "object")
throw new Error("AJAX is not an Object type");

JSAJAX = {
NAME: "AJAX",
VERSION: 1.0,

initAJAX: function(){
    var objxml = null;
    var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];            

    try{
        objxml = new XMLHttpRequest();
    }
    catch(e){                
        for (var i = 0; i < ProgID.length; i++){
            try{
                objxml = new ActiveXObject(ProgID[i]);
            }
            catch(e){                        
                continue;
            }
        }
    }

    return objxml;            
},

getAJAXResponseText: function(xhr){
    var outObj = {};

    outObj.outMsg = "";
    outObj.loadingFlag = false;
    outObj.errorFlag = false;

    if (xhr.readyState == 4){

        if (xhr.status == 200){
            outObj.outMsg = xhr.responseText;
            outObj.loadingFlag = false;
            outObj.errorFlag = false;
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;
        }

    }else{

        if (xhr.status == 200){
            outObj.loadingFlag = true;
            outObj.errorFlag = false;               
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;                
        }
    }

    return outObj;
}
}

And this code here:

window.onload = makeRequest;
var xhr = false;
var currMsg;

function makeRequest() {
currMsg = document.getElementById("updateArea").innerHTML;

xhr = AJAX.initAJAX();

if (xhr) {
    xhr.onreadystatechange = showState;
    xhr.open("GET", "colors.xml", true);
    xhr.send(null);
}
else {
    document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}

function showState() {
var retObj = AJAX.getAJAXResponseText(xhr);

if (retObj.loadingFlag) { // Missing time expiriration of loading
    document.getElementById("updateArea").innerHTML = currMsg + "<h2>Loading...</h2>";
}else{
    document.getElementById("updateArea").innerHTML = currMsg + "<p>" + retObj.outMsg + "</p>";
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-11-25 20:06:35

这是解决方案(感谢@Itay Moav):

if (!AJAX) var AJAX = {};
else if (AJAX && typeof(AJAX) != "object")
throw new Error("AJAX is not an Object type");

AJAX = {
NAME: "AJAX scripts",
VERSION: 1.0,

initAJAX: function(){
    var objxml = null;
    var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];            

    try{
        objxml = new XMLHttpRequest();
    }
    catch(e){                
        for (var i = 0; i < ProgID.length; i++){
            try{
                objxml = new ActiveXObject(ProgID[i]);
            }
            catch(e){                        
                continue;
            }
        }
    }

    return objxml;            
},

getAJAXResponseText: function(xhr){
    var outObj = {};

    outObj.outMsg = "";
    outObj.loadingFlag = false;
    outObj.errorFlag = false;

    if (!xhr){
            outObj.outMsg = "The request has expired";
            outObj.loadingFlag = false;
            outObj.errorFlag = true;

    }else if (xhr.readyState == 4){

        if (xhr.status == 200){
            outObj.outMsg = xhr.responseText;
            outObj.loadingFlag = false;
            outObj.errorFlag = false;
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;
        }

    }else{

        if (xhr.status == 200){
            outObj.loadingFlag = true;
            outObj.errorFlag = false;               
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;                
        }
    }

    return outObj;
}
}

这是:

window.onload = makeRequest;
var xhr = false;
var currMsg;
var timer;

function makeRequest() {
currMsg = document.getElementById("updateArea").innerHTML;

xhr = AJAX.initAJAX();

if (xhr) {
    xhr.onreadystatechange = showState;
    xhr.open("GET", "colors.xml", true);
    timer = setTimeout(function(){xhr = null;}, 2000); // can't delete the object
    xhr.send(null);
}
else {
    document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}

function showState() {
var retObj = AJAX.getAJAXResponseText(xhr);

if (!retObj.errorFlag){
    if (retObj.loadingFlag) {
        document.getElementById("updateArea").innerHTML = currMsg + "<h2>Loading...</h2>";
    }else{
        clearTimeout(timer);
        document.getElementById("updateArea").innerHTML = currMsg + "<p>" + retObj.outMsg + "</p>";
    }
}else{
    document.getElementById("updateArea").innerHTML = currMsg + "<p>" + retObj.outMsg + "</p>";
}
}

Here is the solution (thanks to the @Itay Moav):

if (!AJAX) var AJAX = {};
else if (AJAX && typeof(AJAX) != "object")
throw new Error("AJAX is not an Object type");

AJAX = {
NAME: "AJAX scripts",
VERSION: 1.0,

initAJAX: function(){
    var objxml = null;
    var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];            

    try{
        objxml = new XMLHttpRequest();
    }
    catch(e){                
        for (var i = 0; i < ProgID.length; i++){
            try{
                objxml = new ActiveXObject(ProgID[i]);
            }
            catch(e){                        
                continue;
            }
        }
    }

    return objxml;            
},

getAJAXResponseText: function(xhr){
    var outObj = {};

    outObj.outMsg = "";
    outObj.loadingFlag = false;
    outObj.errorFlag = false;

    if (!xhr){
            outObj.outMsg = "The request has expired";
            outObj.loadingFlag = false;
            outObj.errorFlag = true;

    }else if (xhr.readyState == 4){

        if (xhr.status == 200){
            outObj.outMsg = xhr.responseText;
            outObj.loadingFlag = false;
            outObj.errorFlag = false;
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;
        }

    }else{

        if (xhr.status == 200){
            outObj.loadingFlag = true;
            outObj.errorFlag = false;               
        }else{
            outObj.outMsg = "There was a problem with the request " + xhr.status;
            outObj.loadingFlag = false;
            outObj.errorFlag = true;                
        }
    }

    return outObj;
}
}

And this:

window.onload = makeRequest;
var xhr = false;
var currMsg;
var timer;

function makeRequest() {
currMsg = document.getElementById("updateArea").innerHTML;

xhr = AJAX.initAJAX();

if (xhr) {
    xhr.onreadystatechange = showState;
    xhr.open("GET", "colors.xml", true);
    timer = setTimeout(function(){xhr = null;}, 2000); // can't delete the object
    xhr.send(null);
}
else {
    document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}

function showState() {
var retObj = AJAX.getAJAXResponseText(xhr);

if (!retObj.errorFlag){
    if (retObj.loadingFlag) {
        document.getElementById("updateArea").innerHTML = currMsg + "<h2>Loading...</h2>";
    }else{
        clearTimeout(timer);
        document.getElementById("updateArea").innerHTML = currMsg + "<p>" + retObj.outMsg + "</p>";
    }
}else{
    document.getElementById("updateArea").innerHTML = currMsg + "<p>" + retObj.outMsg + "</p>";
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文