Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

jQuery AJAX request to work in background

Good afternoon,
Ive got myself a nice snippet of code, which works really well to do an AJAX request:-
			<script type="text/javascript">
				$(document).ready(function() {
					$.ajax({
						type: 'GET',
						url: 'viewTree.php',
						dataType: 'html',
						success: function(retHtml, textStatus) {
							alert("Got content back");
							$("#divTreeContent").html(retHtml);
							},
						error: function (xhr, textStatus, errorThrown) {
							$("#divTreeContent").html("Failed - " + errorThrown ? errorThrown : xhr.status);
							}
					})
				});
				</script>

Open in new window


However if the AJAX url takes a long time to load, the page becomes non-reposive.

Is it possible to get fire this off into the background, so the user can continue working?
0
tonelm54
Asked:
tonelm54
  • 6
  • 5
1 Solution
 
leakim971PluritechnicianCommented:
It should be case, comment the alert
0
 
tonelm54Author Commented:
After further reseach I dont think its the AJAX response time, its the putting it into the DIV.

I can get the results after a few seconds (no problems, I just put a loading in), however once the result is returned, it takes 4 minutes to put the result into the DIV, and 400Mb RAM.

Firefox and Opera seem to have the same issue but manage to do it within about 10 seconds.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leakim971PluritechnicianCommented:
yes, loading via ajax don't mean load faster the data
... until you make parallele asynchronous ajax call
0
 
tonelm54Author Commented:
Its like its trying to validate the return before putting it into the div
0
 
tonelm54Author Commented:
Sorry leakim971, if i actually put it as an include, the entire page loads in a few seconds, its just I didnt want the user thinking something bad had happened, so put a waiting screen in, while it pulled the data. Now it takes about 4 minutes to load.
0
 
leakim971PluritechnicianCommented:
This line : $(document).ready(function() {

wait for the complete loading of the page

May help : http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/
0
 
leakim971PluritechnicianCommented:
try this :

                  <script type="text/javascript">
                              $.ajax({
                                    type: 'GET',
                                    url: 'viewTree.php',
                                    dataType: 'html',
                                    success: function(retHtml, textStatus) {
                                          alert("Got content back");
                        $(document).ready(function() {
                                          $("#divTreeContent").html(retHtml);
})
                                          },
                                    error: function (xhr, textStatus, errorThrown) {
                        $(document).ready(function() {
                                          $("#divTreeContent").html("Failed - " + errorThrown ? errorThrown : xhr.status);
})
                                          }
                              })
                        </script>
0
 
tonelm54Author Commented:
Still taking ages.

What I thought Id try and do is record the times at each stage:-
			<script type="text/javascript">
				$(document).ready(function() {
					var reqTime = new date();
					alert("Requesting AJAX");
					$.ajax({
						type: 'GET',
						url: 'viewTree.php?AuthID=2bef0df0-00bd-11e1-afd1-003048c66aaa&UserID=',
						dataType: 'html',
						success: function(retHtml, textStatus) {
							var recTime = new date();
							$("#divTreeContent").html(retHtml);
							var finTime = new date();
							alert("Time requested " + reqTime + "\nTime Recived " + recTime + "\nTime finished " + finTime);
							},
						error: function (xhr, textStatus, errorThrown) {
							$("#divTreeContent").html("Failed - " + errorThrown ? errorThrown : xhr.status);
							}
					})
				});
				</script>

Open in new window


However now the entire AJAX request fails to even start (it never requests the viewTree.php file)
0
 
tonelm54Author Commented:
If I take my records out, so the code now stands at:-
			<script type="text/javascript">
				$(document).ready(function() {
					$.ajax({
						type: 'GET',
						url: 'viewTree.php?AuthID=2bef0df0-00bd-11e1-afd1-003048c66aaa&UserID=6e5289f4-f3f4-11e0-ac3c-003048c66aaa',
						dataType: 'html',
						success: function(retHtml, textStatus) {
							$("#divTreeContent").html(retHtml);
							},
						error: function (xhr, textStatus, errorThrown) {
							$("#divTreeContent").html("Failed - " + errorThrown ? errorThrown : xhr.status);
							}
					})
				});
			</script>

Open in new window


It works fine, but takes ages to load.
0
 
leakim971PluritechnicianCommented:
once you wait for the complete page load (line 2 in yoru last code), it will always take ages to load.
Look like you did not understand or read my comment ID:36956235 and ID:36956318
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now