Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  177] [ 1]  / answers: 1 / hits: 29017  / 15 Years ago, thu, october 15, 2009, 12:00:00

I have this function that makes an ajax call. I'm describing the problem in the last chunk of code comments.



    function doop(){
var that = this;
var theold = theold;
var thenew = thenew;

$.ajax({
url: 'doop.php',
type: 'POST',
data: 'before=' + theold + '&after=' + thenew,
success: function(resp) {
if(resp == 1) {
$(that).siblings('.theold').html(thenew);
}
}
});

// I have some code here (out of the ajax) that **further** changes
// the .theold's html beyond what it was changed inside ajax success
// but the change depends on whether the resp (inside the success
// function) returned 1 or not, so this code out here depends on the ajax
// so it looks like I have to turn this ajax call into a sync ajax

return false;
}


Based on the problem as described in the code comments, what changes are best for this situation?


More From » jquery

 Answers
4

You need to set async: false for synchronous requests like this:



function doop(){
var that = this;
var theold = $(this).siblings('.theold').html();
var thenew = $(this).siblings('.thenew').val();

$.ajax({
async: false,
url: 'doop.php',
type: 'POST',
data: 'before=' + theold + '&after=' + thenew,
success: function(resp) {
if(resp == 1) {
$(that).siblings('.theold').html(thenew);
}
}
});

// some other code

return false;
}


see here for details


[#98507] Saturday, October 10, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;