我正在使用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响应吗?
答案 0 :(得分:2)
您可以使用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 */
}
}
});