2

I am working with this API, which returns flight statuses originating in LHR to MAN (just an example). The response is formatted as XML, but I'm having trouble reading it with JavaScript.

I tried the following:

function loadXMLDoc(dname) { //the dname here is the url
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    } else {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET", dname, false);
    xhttp.send();
    return xhttp.responseXML;
}

But it doesn't work. Is there another way to read the XML response of the API?

Kira
  • 955
  • 1
  • 11
  • 25

1 Answers1

3

You could use jQuery and then make your ajax call using jQuery.ajax()

The code might look something like this...

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: url,
        data: JSON.stringify(data),
        dataType: "json",
        error: function (response) {
            alert('Error: There was a problem processing your request, please refresh the browser and try again');
        },
        success: function (response) {
            if (response !== undefined && response !== null) {
               /* Evaluate the SOAP response object here */
            }
        }
    });
matzoi
  • 66
  • 1
  • So this is if the function is returns in json i im not mistaken..? and could you explain the evaluate SOAP object here. Im new to all this. @matzoi – Kira Aug 06 '13 at 17:17
  • 1
    The SOAP response XML will be serialized and accessible in the local variable named 'response'. It will have properties named as all the child nodes of response in that object. Therefore you will have properties named error, appendix, flightStatuses, etc. – matzoi Aug 06 '13 at 17:47
  • I tried it in a javascript function but nothing happens. @matzoi i used the above code inside a javascript function but it doesnt return anything. I just put an alert box if there is success.. but it returns nothing. – Kira Aug 07 '13 at 06:31
  • 1
    You will first need to include the jQuery libraries [link] (http://stackoverflow.com/questions/547384/where-do-you-include-the-jquery-library-from-google-jsapi-cdn). I also recommend you read a good jQuery tutorial such as this [link] (http://jqfundamentals.com/legacy) – matzoi Aug 07 '13 at 16:05
  • thanks man i got it working was getting a cross platform error or something.. changed it to jsonp and it works fine now!! – Kira Aug 07 '13 at 16:15