Solved

make jquery to wait for .get function

Posted on 2011-09-09
5
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

749 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