0

Posting this question again, as the code is completely different this time.

Trying to get flight information in XML format from Avinor (Norwegian aviation authorities). Example of query: http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=7&airport=OSL&direction=D&lastUpdate=2016-04-04T15:03:00Z

I've made an Ajax GET query in order to parse the xml feed into my html p with id=flights, but with no success. I get this error in Firebug console:

SyntaxError: expected expression, got '<'

Firebug->Net->Headers displays the following:

Response headers
Cache-Control   private
Content-Length  28088
Content-Type    text/xml; Charset=iso-8859-1
Date    Mon, 04 Apr 2016 18:32:07 GMT
Server  Microsoft-IIS/7.0
X-Powered-By    ASP.NET

Request headers
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Host    flydata.avinor.no
User-Agent  Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0

When I go into Firebug->Net->XML, I can see the XML data just fine. Can anyone please help me figure this out?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>

<button type="button" onclick="loadFlights()">
Get Flights</button>

<p id="flights"></p>

<script>

function loadFlights() {

 var url = "http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=7&airport=OSL&direction=D&lastUpdate=2016-04-04T15:03:00Z";
  $.ajax({
    url: url,
    type: "GET",
    dataType: "jsonp",
    success: function myFunction(xml) {
  var x, i, xmlDoc, txt;
  xmlDoc = xml.responseXML;
  txt = "";
  x = xmlDoc.getElementsByTagName("flight_id");
  for (i = 0; i< x.length; i++) {
    txt += x[i].childNodes[0].nodeValue + "<br>";
  }
  document.getElementById("flights").innerHTML = txt;
}
  }); 
};

</script>

</body>
</html>
  • If your dataType is jsonp your url should be pointing to a javascript file not an xml file. BTW SOP would be totally useless if you could bypass it with a single line of code. – Musa Apr 04 '16 at 19:24

1 Answers1

1

You are asking for JSONP — which AJAX would accept because padded JSON is the only thing you can get cross-domain with an AJAX call. However you are not getting JSON back; you are getting XML. And to make matters even more interesting you are trying to parse it as HTML.

From another SE answer.

Finally, let's move onto the main question: Can JSONP be used to fetch XML, or can we parse XML cross-domain? The answer, as others have pointed out, is a resounding NO

So you are outta luck there. Sorry.

Community
  • 1
  • 1
MichaelK
  • 2,859
  • 1
  • 14
  • 16
  • I see. AJAX call not working due to CORS not implemented on server side. What I don't understand, is that I can see the xml data I get from the server in Firebug, right-click and save it as the suggested filename (XmlFeed.asp.xml). In my head, that means I got the xml data I requested to my computer. – user3434120 Apr 05 '16 at 16:49
  • @user3434120 Oh it is not that the *call* is not made. But the parser expects JSONP, so when it gets XML instead, naturally it gets upset and throws a great big sulk. The big problem for you is — of course — that JSONP is the only thing you *can* request cross-domain. – MichaelK Apr 05 '16 at 18:50
  • Thanks for clarifying this, Michael. Just tried with a getJSON call, using the service over at AnyOrigin, and it worked. Wonder how they do it... :-) – user3434120 Apr 06 '16 at 00:33