简体   繁体   中英

JQuery calling XML webservice

I'm trying to practice jquery with webservices and callign on of he open xml service from US Airports.

webservice url is http://services.faa.gov/airport/status/IAD?format=application/xml

and my Query code as below but when the page is loaded it shows an empty screen :( can someone guide me please. I searched online and couldnt' figure out.

<html>
<head>
    <script type="text/javascript" src="assets/jquery.js"></script>
    <title>Aviation</title>
    <script type="text/javascript">
        $(document).ready(function () {
        $.ajax({
        type: "GET",
        url: "http://services.faa.gov/airport/status/IAD?format=application/xml",
        dataType: "xml",
        success: xmlParser
        });
    });

    function xmlParser(xml) {

        $('#airport').fadeOut();
        $(xml).find("AirportStatus").each(function () {
        $("#details").append($(this).find("ICAO").text() + "</br>"+ $(this).find("State").text());
        //$(".book").fadeIn(1000);
        });
    }
    </script>
</head>
<body>
    <p id="airport">Loading...</p>
    <p id="details"></p>
</body>
</html>

Thanks for your time in advance.

its restricted by CORS you cannot access data across the domain, moreover the xml seems to have some formatting issues try the json format along with dataType:'jsonp'

$.ajax({
        type: "GET",
        url: "http://services.faa.gov/airport/status/IAD?format=json",
        dataType: "jsonp",
    success: function(data){
        alert("asd");
    console.log(data);
    }
        });

http://jsfiddle.net/WxMXR/7/

There are error when I execute you js in http://jsfiddle.net/QYQ4V/2/

Because you are not allowed to use the ajax calls to fetch data from other domains ... (unless using the JSONP or script datatypes ..) JSONP: http://en.wikipedia.org/wiki/JSONP

So that mean you can use other language(python/ruby/java) to get the data and use jquery to fetch the data in the same domain rather than use XHR($.ajax) in jQuery.

FYI

The server side return xml and response http header 'Content-Type'='application/xml' which should be 'text/xml',

and low level XMLHttpRequest using 'application/xml', so that conflicts happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM