php – XMLHttpRequest在IE 7/8中不起作用,但在其他浏览器中有效

前端之家收集整理的这篇文章主要介绍了php – XMLHttpRequest在IE 7/8中不起作用,但在其他浏览器中有效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开发了一个适用于chrome和firefox的web应用程序.但是,当涉及到测试时间并且它在IE中无法正常工作时.它似乎没有真正得到请求?

这是Javascript代码

function createXMLHttpRequest() {
    var xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlhttp = false;
            }
        }
    }
    return xmlhttp;
};




function checkForData(){

    var target = document.getElementById('accordion');
    var loading = document.getElementById('controls');

    var xhr = createXMLHttpRequest();


    loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Querying the Database';

    xhr.open('GET','getData.PHP',true);
    xhr.send(null); 

    xhr.onload = function(){
        loading.innerHTML = '<a href="../classes/PHP/print.PHP" />Print Report</a><br/><br/><a href="../index.PHP" />HomePage</a><br/><a href="../classes/PHP/actionedClass.PHP" />Refresh</a><br/><a href="../classes/PHP/logout.PHP" />logout</a><br/>';

        target.innerHTML = xhr.responseText;
//      addPrettyness();

    };
    xhr.onerror = function (){
        target.innerHTML = 'Failed :(';
    };

}

function addPrettyness(){
    $(function() {
                    var stop = false;
                    $( "#accordion h3" ).click(function( event ) {
                        if ( stop ) {
                            event.stopImmediatePropagation();
                            event.preventDefault();
                            stop = false;
                        }
                    });
                    $( "#accordion" )
                            .accordion({
                                collapsible: true,header: "> div > h3"});
    });
}

function checkAgain(){
    setInterval('checkForData()',30000);            //Every 30 seconds run the check for new data script

}


function changeAction(action,actionChangeId){

    xhr = new XMLHttpRequest();

    if(action == 0){


        var loading = document.getElementById('controls');
        loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Sending to the Database';        

        xhr.open('GET','../classes/PHP/actionNo.PHP?actionChangeId='+actionChangeId,true);
        xhr.send();

        xhr.onload = function(){
            checkForData();
        };

        xhr.onerror = function(){
            alert('Failed to change action... Try again please!');
        };

    }else if(action == 1){
        xhr = new XMLHttpRequest();

        var loading = document.getElementById('controls');
        loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Sending to the Database';        


        xhr.open('GET','../classes/PHP/actionYes.PHP?actionChangeId='+actionChangeId,true);
        xhr.send();

        xhr.onload = function(){
            checkForData();
        };

        xhr.onerror = function(){
            alert('Failed to change action... Try again please!');
        };
    }
}

function startEngine(){
    checkForData();
    //checkAgain();

}


window.onload = startEngine;

它从echos请求的.PHP文件将字符串结果返回给javascript.

here所述,Internet Explorer仅支持版本9以来的XMLHttpRequest对象的onload事件.

所以,对于IE 8及更低版本,你可以用老式的方式做到:

xhr.onreadystatechange = function()
{
    //ready?
    if (xhr.readyState != 4)
        return false;

    //get status:
    var status = xhr.status;

    //maybe not successful?
    if (status != 200) {
        alert("AJAX: server status " + status);
        return false;
    }

    //Got result. All is good.
    loading.innerHTML = '<a href="../classes/PHP/print.PHP" />Print Report</a>' + 
        '<br/><br/><a href="../index.PHP" />HomePage</a><br/>' + 
        '<a href="../classes/PHP/actionedClass.PHP" />Refresh</a><br/>' + 
        '<a href="../classes/PHP/logout.PHP" />logout</a><br/>';
    target.innerHTML = xhr.responseText;

    return true;
}
原文链接:https://www.f2er.com/php/135514.html

猜你在找的PHP相关文章