简体   繁体   中英

Javascript XMLHTTP request only works once?

This is a part of my code:

function addText(id, type){
    alert("ID="+id);
    alert("TYPE="+type);

    var title = getData("title", id);
    var text = getData("text", id);
    var imageUrl = getData("imageUrl", id);

    document.getElementById("contentArea").innerHTML += '<div id="' + id + '"> </div>';

    if(type == 1){
        allText = html1.responseText;
        titleReplace = "K" + id + "title";
        textReplace = "K" + id + "text";
        imageReplace = "K" + id + "image";
        allText.replace("K1title", titleReplace);
        allText.replace("K1text", textReplace);
        allText.replace("K1image", imageReplace);
    }

    document.getElementById(id).innerHTML = allText;

    document.getElementById(titleReplace).innerHTML = title;
    document.getElementById(textReplace).innerHTML = text;
    document.getElementById(imageReplace).src = imageUrl;

}

function getData(Parameter, id){
    var result = undefined;
    var URL = phpGetURL + "?par=" + Parameter + "&id=" + id;

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET', URL ,false);
    xmlhttp.send();
    result = xmlhttp.responseText;
    xmlhttp.abort();

    //lert(result);
    return result;
}

And this is a part from the index.php where is call the function 4 times:

<script type="text/javascript" src="js/dataManager.js"></script>
<script type="text/javascript"> addText(1, 1); addText(2, 1); addText(3, 1); addText(4, 1); </script>

The first one displays, the second one displays but not with the replaced text. The fourth and the fifth doesn't display at all.

It's gets the data from a php script that gets it from a MYSQL database.

I don't get it to work.

thx

I had the same issue while working on an extension for chrome. You should may try to initialize the "XMLHttpRequest()"-instance at the very beginning of your code, so that is only created once.

I'm posting this for others, because i've read that you had to solve this problem fast, years ago... So it is not urgent anymore i guess...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM