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

标签 javascript xml api response

我正在与 this API 合作,它将返回从 LHR 出发至 MAN 的航类状态(仅作为示例)。响应的格式为 XML,但我在使用 JavaScript 读取它时遇到问题。

我尝试了以下方法:

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;
}

但是这不起作用。还有其他方法可以读取 API 的 XML 响应吗?

最佳答案

您可以使用 jQuery,然后使用 jQuery.ajax() 进行 ajax 调用

代码可能看起来像这样......

    $.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 */
            }
        }
    });

关于javascript - 如何在 JavaScript 中解析来自 API 的 XML 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18085310/

相关文章:

java - 如何在 Netbeans 8.2 中使用 XSD 1.1?

java - 如果在另一个 TextView 中更改文本,则 TextView 文本动画将重新启动

javascript - 除非刷新页面,否则 jQuery Ajax 点击不起作用

javascript - 如何使用 Angularjs 指令根据动态值更改文本颜色?

javascript - 奇怪的 jQuery $.each() 行为

node.js - 如何向 Heroku 添加 API key ?

c# - 如何使用位置用户名或 ID 获取在特定位置的 Instagram 中上传照片的所有用户?

javascript - 对象 HTMLImageElement 显示而不是图像

python - 使用 Python 在 ElementTree 中添加兄弟元素

android - 无法使用groundOverlay在android中的google map api v2上显示图像