0

I wrote following Code which runs nicely on the old Internet Explorer, but not on Firefox, Chrome, Edge etc. Is the problem the fact that I am importing the json via http and not https? And if so, how can I adjust this? Any help is appreciated!

<head>
    <script>
        var myObj, x, txt = "";
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                myObj = JSON.parse(this.responseText);
                txt += "<table border='1'>"
                for (x in myObj.Data) {
                    txt += "<tr><td>" + myObj.Data[x].Name + "</td></tr>";
                }
                txt += "</table>"
                document.getElementById("demo").innerHTML = txt;
            }
        }
        xmlhttp.open("GET", "http://134.255.254.137/Data.json");
        xmlhttp.send();
    </script>
</head>
<html>

<body>
    <div class="container">
        <h2>Daten aus JSON-Datei:</h2>
        <div id="demo">
        </div>
    </div>
</body>

</html>
Jay
  • 11
  • 2
  • Are you loading this page on that same IP ? Any errors in browser console? Also should either move the JS code below the element `id="demo"` or put it in a load event handler to make sure the element exists when request completes – charlietfl Aug 29 '18 at 22:16
  • No, I am using a different IP. I know that the element exists as I put it there. I think the problem is that newer browser block parts of the code. – Jay Aug 29 '18 at 22:22
  • 1
    If it is different IP then you need to implement CORS if you control the back end...or use a proxy. Also code that runs above an element in a page will not find that element. The delay in the XMLHttpRequest might mean it will exist when you actually look for it but that is not a guaranteed thing. See [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – charlietfl Aug 29 '18 at 22:24
  • Depending on the website settings, if the website is running as HTTPS it may only allow data from other HTTPS sources. Is the JSON you are trying to fetch available at the HTTPS version of that resource? (I'm assuming no or you wouldn't be asking, but just making sure.) Do you control the entire website and its settings? – Matthew Herbst Aug 29 '18 at 22:35
  • I dump the json with a python script. The json is saved on a server. I want to implement that json in a table on a website (wordpress). But even when I just run the script as above (double clicking html file) it doesnt work (only in old Internet Explorer). So the problem exists even without any website involved. – Jay Aug 29 '18 at 22:39
  • So I just saw that the console in Chrome gives me this: Untitled-2.html:1 Failed to load http://134.255.254.137/Data.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. What is that supposed to mean? I guess I can only load ressources via HTTPS and not HTTP – Jay Aug 30 '18 at 11:20

0 Answers0