/    Sign up×
Community /Pin to ProfileBookmark

Ajax issue with multiple requests…

Hi,
I wonder if someone can help me out. I have a jsp page using ajax that has a button with an action. The action sends multiple ajax requests. The response from these requests is to be used to update a progress indicator to show the servers current progress. I can see from the debug that I am getting the 1st response. I think it is the way how I have implemented the further requests. I think this is down to my javascript knowledge, which isnt very much.

Also I assume using multiple requests in this way is the correct thing to do???

I have spent ages trawling the internet trying to get a solution. Hope someone can help out.

Paul.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@DanInMAAug 19.2011 — your goign to have to show your code on this one.
Copy linkTweet thisAlerts:
@pbdeveloperauthorAug 19.2011 — your goign to have to show your code on this one.[/QUOTE]

I have included some code below to show an example. Upon loading the page the server is requested to progress. The initial state is displayed ok. ?

The server then processses and updates the text file (updateStatus.txt) file. I can see this by directing the browser to the file.

My the web page doesnt update though. I expect this because I only have one ajax call.

The server finishes processing.

When I click the button the div is updated with the last message. Again this is what I expect. ?

So, I would be extremely grateful if someone please tell me how do I go about doing multiple requests.

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="updater"
class="com.product.Updater" scope="page" />
<jsp:setProperty name="updater" property="servletContext"
value="<%=getServletContext()%>" />
<html>
<head>
<script type="text/javascript">
var xmlhttp;

function getUpdates() {
var c = 0, t = 20;
// Create a timer
window.setInterval(function() {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}, 50);
}

function loadXMLDoc() {
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("GET", "updateStatus.txt", true);
xmlhttp.send();
}

</script>
</head>
<body onLoad="getUpdates();loadXMLDoc();<%updater.start();%>">
<div id="myDiv">
<h2>AJAX do a test</h2>
</div>
<button type="button" onclick="loadXMLDoc()">Change
Content</button>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@pbdeveloperauthorAug 21.2011 — Can someone please help me out with this?

Thanks,

Paul
Copy linkTweet thisAlerts:
@DanInMAAug 21.2011 — do you mean it is not updating automatically? it only updates when you clcik change content?

if so this is because getUpdates() will not work. the response text is not a global variable to getupdates cannot access it. I thik what you need to do is set a timer on the loadxmldoc function instead of how you are doing it now
×

Success!

Help @pbdeveloper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...