0

I have asked an earlier question here (Synchronous XMLHttpRequest issue) but it was closed due to "duplicate". I have tested the duplicate answer and it does not solve the issue.

Issue

I am trying to read XML data and I am receiving warning - Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. , upon the duplicate answer posted (How to get the response of XMLHttpRequest?), which provides solution that I must change the xml.open() from "false" to "true" in order to stop the warning from happening.

I did that and yes it solved the problem of not receiving any warning message but I can no longer receive any XML data. I have tested with xml.responseText and I receive empty response. So what is causing this issue and how do I solve this without using AJAX (most of the online answers have AJAX implemented in them).

<script>
    var xml = new XMLHttpRequest();
    xml.onreadystatechange = function() {
    if (xml.readyState == XMLHttpRequest.DONE) {
        }
    }

    xml.open('GET', 'PDF_List.xml', false); 
    xml.send(null);
    var xmlData = xml.responseXML;
    if(!xmlData){
        xmlData = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');
        var PDF = xmlData.getElementsByTagName("PDF");
        
        var Link = PDF[0].getElementsByTagName("link")[0].firstChild.data;
        
    }
</script>
<?xml version = "1.0" ?>

<List>
    <PDF>
        <id>1</id>
        <link>myPDF.pdf</link>
    </PDF>
</List>

</xml> 
Arthur
  • 45
  • 5
  • 1
    If you change `open` to true, you can then put the logic that relies on `responseXML` in your if-statement of your callback: `if (xml.readyState == XMLHttpRequest.DONE) {`. You can find an example [here](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange) – Nick Parsons Apr 10 '21 at 00:18
  • 1
    @NickParsons thanks alot! That worked. I had tried that earlier but because I was also adding ```xml.open('GET', 'PDF_List.xml', true);``` inside the onreadystatechange() function it was not reading it. – Arthur Apr 10 '21 at 00:21
  • 1
    This is explicitly explained in the duplicate of your previous question. "_You can get it by `XMLHttpRequest.responseText` in `XMLHttpRequest.onreadystatechange` when `XMLHttpRequest.readyState` equals to `XMLHttpRequest.DONE`._" I'd say the duplicate still applies. – Ivar Apr 10 '21 at 00:30

0 Answers0