0

I am trying to read XML data and I am receiving an error such as Synchronous XMLHttpRequest on the main thread is deprecated.... I have searched online and got to know that I must put the async to true but still it does not solve the issue. I am still not able to read the data. How to solve this?

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    </head>
</html>

<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;

        console.log(link);
        
    }
</script>

<?xml version = "1.0" ?>
<List>
    <PDF>
        <id>1</id>
        <link>myPDF.pdf</link>
    </PDF>
</List>

EDIT: After referring to the duplicate link posted above, the solution given was to add "true" in xml.open() so that no async request is waiting to be received. But if I change it to true, I no longer receive the warning but then I am not able to receive any data from XML. I have tested it and it shows empty value under xml.responseText. I have even updated my code above. How do I remove this warning?

Arthur
  • 45
  • 5
  • Why do you use `jQuery.ajax` if you're using `XMLHttpRequest` to make the request? Drop the`$.ajax({...})` part since that doesn't do anything. – goto Apr 09 '21 at 23:28
  • `$.ajax` is jquery, you need to change the `false` argument in your `open()` call, and use a callback to wait for your request to finish and get the data, eg [`xml.addEventListener('load',()=>{})`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onload) – Patrick Evans Apr 09 '21 at 23:29
  • If you're using native XMLHttpRequest, then you need to use the "onreadystatechange" handler to determine when the async request has completed (and determine the completion status). https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange – Craig Apr 09 '21 at 23:29
  • @goto1 Yes I did that before. I thought it must be added to avoid the warning, and even if I do remove it, i am still not able to read the data. – Arthur Apr 09 '21 at 23:30
  • @Arthur your `XMLHttpRequest` code is incorrect, that's what needs to be fixed. See the links above. – goto Apr 09 '21 at 23:31
  • Thanks I added the XMLHttpRequest() from the answer posted, now I am getting an error as Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined on ```var link = PDF[0].getElementsByTagName("link")[0].firstChild.data;``` What am I doing wrong here? – Arthur Apr 09 '21 at 23:33
  • 1
    @Arthur It sounds as if the loaded file doesn't have a `` tag. You might want to see what `xml.responseText` contains to make sure that it does. – Ivar Apr 09 '21 at 23:36
  • @Ivar You are right, the xml.responseText is empty. Even though I followed the structure. Any idea why? – Arthur Apr 09 '21 at 23:39
  • Ah got it, i had to set bool to false here - xml.open('GET', 'PDF_List.xml', false); – Arthur Apr 09 '21 at 23:40
  • I am sorry but I am still receiving the warning. I have updated my question, kindly take a look. One way to remove this warning is if I change ```xml.open('GET', 'PDF_List.xml', false);``` to true, but then I do not receive any data from the xml. – Arthur Apr 09 '21 at 23:56
  • You have to move `var xmlData = xml.responseXML;` and the rest of the code below it to that readystate callback – Patrick Evans Apr 11 '21 at 01:17

0 Answers0