Reputation: 5407
I tried to perfrom two sequential ajax request like:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","data.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('url=' + url);
var x=10;
var y=20;
xmlhttp.open("POST","datatest.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('x=' + x, 'y=' + y);
Does not Give error, but says:
POST http://dev01.dev/data.php Aborted
And shows result of echo
in datatest.php only. HOw can i get response from both data.php and datatest.php?
UPDATE:
data.php
will give some result.
echo $result1;
datatest.php
will give some result.
echo $result2;
I want to append above two result in myDiv
.
If I do
document.getElementById("myDiv").innerHTML=xmlhttp.responseText1;
then
document.getElementById("myDiv").innerHTML=xmlhttp.responseText2;
It will replace the content. I want to append it!
Upvotes: 0
Views: 1076
Reputation: 1074148
HOw can i get response from both data.php and datatest.php?
Use a separate XHR object rather than attempting to reuse the existing one. Your current code starts a request, but then you do xmlhttp.open("POST","datatest.php",true);
on that same XHR object again before the request has time to finish, so it gets aborted.
For example:
var xmlhttp1, xmlhttp2;
function getXHR() {
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
else { // code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
xmlhttp1 = getXHR();
xmlhttp1.onreadystatechange = function () {
if (xmlhttp1.readyState == 4 && xmlhttp1.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp1.responseText;
}
};
xmlhttp1.open("POST", "data.php", true);
xmlhttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp1.send('url=' + url);
var x = 10;
var y = 20;
xmlhttp2 = getXHR();
xmlhttp2.onreadystatechange = function () {
// Presumably do someething with the result of this one, too
};
xmlhttp2.open("POST", "datatest.php", true);
xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp2.send('x=' + x, 'y=' + y);
Upvotes: 1