0
我不得不寫在單一功能的兩頁的Ajax調用後二Ajax響應比較是沒有任何替代方法來編寫代碼,這個也是我想比較這兩個Ajax響應,即,比較這兩個Y2和Y3對象。如何解析
function compareResponses() {
alert('comp');
// Getting object of ORIGINAL data
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
y = xmlhttp.responseText;
alert(y);
y2 = JSON.parse(y);
}
}
xmlhttp.open("POST", "2.ashx", true);
xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
// Getting object of another data
if (window.XMLHttpRequest) {
xmlhttp1 = new XMLHttpRequest();
} else {
xmlhttp1 = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp1.onreadystatechange = function() {
if (xmlhttp1.readyState == 4 && xmlhttp1.status == 200) {
y1 = xmlhttp1.responseText;
//alert(y1);
y3 = JSON.parse(y1);
}
xmlhttp1.open("POST", "1.ashx", true);
xmlhttp1.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xmlhttp1.send();
}
你想檢查它們是否平等嗎? –
您錯過了該功能的右大括號。 – Barmar
如果你想同時發送兩個AJAX請求,我認爲你需要使用的承諾,等待他們倆來完成。相反,你可以連續發送它們。第一個請求的回調函數發出第二個請求,第二個回調函數可以比較響應。 – Barmar