0
votes

I'm parsing some XML returned back from an AJAX request but I have a special case I have to check for before parsing. Sometimes the server will return some HTML and I need to perform a reload on the page if this is the case. The problem is that when I try to do a simple check for the META tag that's in the head element, I keep getting nothing returned.

Below is an example of the html that is returned.

<html>
    <head>
    <title>Redirection</title>
    <META HTTP-EQUIV="REFRESH" URL="https://testurl/desktop"/> 
    <META HTTP-EQUIV=Pragma CONTENT=no-cache>
    </head>
</html/>

The below is a snippet of the jQuery that I'm using to try and debug. The xml var is what's returned from the success method of the $.ajax() call.

parseXml(xml) {
    $(xml).parent().find('META').each(function() {
        location.reload();
    });
}

The .find() method never finds the META tag and exits and causes my code to break. Any suggestions would be very much appreciated.

Here is the ajax call with $.ajax({ type: 'POST', url: '?action=someAction&target=someTarget', success: function(response) { try { var orders = parseXml(response); } });

Here is sample xml that I usually get back

<?xml version="1.0" encoding="ISO-8859-1"?>
<ORDERS>
    <ORDER STATUS="OPEN" ID="62452254" DATE="May 5" />
    <ORDER STATUS="FILLED" ID="341411" DATE="May 8" />
</ORDERS>
1
Need to see your full ajax options.Beetroot-Beetroot
It also depends on how your xml is structured. Can you also show xmldefau1t
I edited the initial post to include ajax options and sample xmlJustin

1 Answers

0
votes

I don't think you need .parent(), and you probably need to pull out the URL attribute inside the function.

EDIT: Grabbing more space to experiment from comments:

alert(xml);
var h = $(xml).html();
alert(h);

The former alert shows the content, the latter alert shows null. Jquery selector for <link> elements in <head> seems to have an elegant answer in the accepted answer:

From the [jQuery] documentation http://api.jquery.com/jQuery/#jQuery2

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, we use the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as , , or elements. As a result, the elements inserted may not be representative of the original string passed.