Solved

make jquery to wait for .get function

Posted on 2011-09-09
5
547 Views
Last Modified: 2012-05-12
Hello.

I have this function
function runAjaxCap(mycode) {
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html(fileContents);});

}

Open in new window


I need to wait until
jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html

Open in new window

has change my div content.

Please HELP.
Thanks
0
Comment
Question by:radugheorghies
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36513815
use $.ajax (or use jQuery.ajaxSetup) to make a synchrone ajax call :

function runAjaxCap(mycode) {
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	var fileContents = jQuery.ajax({ "url":xaa "async":false }).responseText;
        jQuery("#captchacatcher").html(fileContents); 
}

Open in new window


more info : http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36513838
try:
function runAjaxCap(mycode) {
//force it to wait for get completion
jQuery.ajaxSetup({async:false});
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html(fileContents);});

//restore default so it won't "break/interfere" with other ajax calls
jQuery.ajaxSetup({async:true});

}

Open in new window

0
 

Author Comment

by:radugheorghies
ID: 36513863
Sorry leakim971

something is wrong here:
var fileContents = jQuery.ajax({ "url":xaa "async":false }).responseText;

thanks for answering.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36513877
add a comma after xaa

var fileContents = jQuery.ajax({ "url":xaa, "async":false }).responseText;
0
 

Author Closing Comment

by:radugheorghies
ID: 36513883
Thank you.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question