我正在与 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/