XMLHttpRequest() , Why does it not work..?



  • Hi All,
    I’m building my first app in the Monaca IDE. I want to include an external db, but i go step by step.

    I use an example from w3schools, see below.

    It works fine with a local text file (that is: works in the IDE preview, not in the debugger).

    It doens’t work when i copy the file to a website and change the URL accordingly to:

    var url = "http://learningspaceship.com/myTutorial.txt";

    The app (in browser and in debugger) does not see the file. But i can see the file in my browser.

    What to do…?

    SCRIPT FROM W3SCHOOL.COM

    <div id=“id01”></div>

    <script>
    var xmlhttp = new XMLHttpRequest();
    var url = “myTutorial.txt”;

    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var myArr = JSON.parse(xmlhttp.responseText);
    myFunction(myArr);
    }
    };
    xmlhttp.open(“GET”, url, true);
    xmlhttp.send();

    function myFunction(arr) {
    var out = “”;
    var i;
    for(i = 0; i < arr.length; i++) {
    out += ‘<a href="’ + arr[i].url + ‘">’ +
    arr[i].display + ‘</a><br>’;
    }
    document.getElementById(“id01”).innerHTML = out;



  • i did see the post on the Cordova Whitelist, but is says: By default, new apps are configured to allow access to any site.

    Maybe it’s not so…



  • No, it is true. In the XML config it says: <access origin="*"/> already.


Log in to reply