简体   繁体   English

如何在 JavaScript 中解析来自 API 的 XML 响应?

[英]How to parse XML response from API in JavaScript?

I am working with this API , which returns flight statuses originating in LHR to MAN (just an example).我正在使用这个 API ,它将源自 LHR 的航班状态返回给 MAN(只是一个例子)。 The response is formatted as XML, but I'm having trouble reading it with JavaScript.响应的格式为 XML,但我无法使用 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?是否有另一种方式来读取 API 的 XML 响应?

You could use jQuery and then make your ajax call using jQuery.ajax()您可以使用 jQuery,然后使用jQuery.ajax()进行 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 */
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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