Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
AJAX JQuery nested call
Old 12-16-2010, 11:42 AM AJAX JQuery nested call
Novice Talker

Posts: 6
Trades: 0
Hi guys,

Please refer the code below,
I already set both of the ajax as async : false.
I found out that when the function calling doUpdate2ndAJAX(), the program start executing $(".search").click(); even though doUpdate2ndAJAX() not yet finish executing.

In short current situation:
1. doUpdate2ndAJAX() execute
2. doUpdate2ndAJAX() not yet finish, start execute $(".search").click()
3. doUpdate2ndAJAX() Finish execute and response back

May I know how can I make it as
1. doUpdate2ndAJAX() execute
2. doUpdate2ndAJAX() Finish execute and response back
3. call $(".search").click();

[ Execute in sequence order ]

Thanks.
-fsloke

Code:
firstCalled: function() {
             $.ajax({						   
			url: "XXX",
			async: false,
			success: function(response) {

                        doUpdate2ndAJAX();

                        $(".search").click();
                        
                        }
	       });
}				

function  doUpdate2ndAJAX(){
              $.ajax({						   
			url: "YYY",
			async: false,
			success: function(response) {
                            // UPDATE SOMETHING
                        }
               });
              return false;
}
fsloke is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-16-2010, 02:39 PM Re: AJAX JQuery nested call
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You'll need to put the $(".search").click(); into the callback (success) of the doUpdate2ndAJAX(). If you just put it second in line after an AJAX call, it won't wait for that call to complete to execute.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 12-16-2010, 09:13 PM Re: AJAX JQuery nested call
Novice Talker

Posts: 6
Trades: 0
Hi wayfarer07,

Can coded as below?
But it does not work accordingly.

Code:
firstCalled: function() {

               $.ajax({						   
			url: "XXX",
			async: false,
			success: function(response) {
              
              var isCheck;
              $.ajax({						   
			url: "YYY",
			async: false,
			success: function(response) {
                            // UPDATE SOMETHING
                           
                            isCheck = response; // respone either true/ false
                        }
               });

               if(isCheck =="true"){
                     $(".search").click(); 
               }else{
                    alert("ERROR");
               }
 });


}
Thanks
fsloke is offline
Reply With Quote
View Public Profile
 
Old 12-16-2010, 09:57 PM Re: AJAX JQuery nested call
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You have a syntax error. I pasted your code into my editing tool, which has automatic error detection, and it said there is a closing bracket missing at the end.

Here is the corrected code:

Code:
firstCalled: function() {

    $.ajax({                           
        url: "XXX",
        async: false,
        success: function(response) {

            var isCheck;
            $.ajax({                           
                url: "YYY",
                async: false,
                success: function(response) {
                    // UPDATE SOMETHING

                    isCheck = response; // respone either true/ false
                }
            });

            if(isCheck =="true"){
                $(".search").click(); 
            }else{
                alert("ERROR");
            }
        }
    });
}
I assume this code is a part of an object list? If not, you need to define it some other way. I'm talking about this syntax here:
Code:
firstCalled: function() {}
This syntax is only valid when indicating a field in an object. If you're defining a normal function this won't work. So a correct usage would be:
Code:
var my_object: {
    firstCalled: function() {/*a bunch of code*/},
    some_property: "string value"
}
then you could call the function like so:
Code:
my_object.firstCalled();
but if you just want a normal function name you must do:
Code:
function firstCalled() {
    /*code*/
}
or
Code:
var firstCalled = function() {
    /*code*/
}
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 12-19-2010, 11:04 AM Re: AJAX JQuery nested call
Novice Talker

Posts: 6
Trades: 0
What different between complete and success?

I confuse every method need to be success in order to get return message..... why complete exist?

Thanks
fsloke is offline
Reply With Quote
View Public Profile
 
Old 12-19-2010, 12:56 PM Re: AJAX JQuery nested call
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by fsloke View Post
What different between complete and success?

I confuse every method need to be success in order to get return message..... why complete exist?
To understand this, you need to realize that when you make an AJAX call, you are making a complete HTTP request to the server. HTTP protocol, under normal circumstances, requires that the client wait for the transmission of data to be complete before processing it. The "A" in AJAX stands for Asynchronous, which means occurring independently in the main line of logic. Because the time that it takes to receive the result of the HTTP request is non-deterministic, if it it was the opposite (Synchronous), it would lock all other logic while waiting for the results to be transmitted, which is usually not what we want.

So this means, there is always a function defined to deal with the results of an AJAX call, if you need to know the results. In jQuery this is handled by either the success or complete options. The success function only fires if the request is completed successfully, while the complete function will fire when the HTTP request completes, even if it fails. Since there will be no data upon failure, normally you should be using success.

However, I usually use shorthand methods, which are easier to nest, and easier (to me) to read:

Code:
$.get('/path/to/script.php',//URL to send to
    {param1: 'somedata', param2: 'somedata'},//data to send
    function(data) {//success function
        if(data) {//only execute next AJAX call if data exists
            $.get('path/to/script2.php'),//second URL to send to
            {param1: data},//second data to send
            function(data) {//second success function
                //do something with resulting data of nested AJAX call
            }
        }
    }
);
$.get is shorthand for making an HTTP GET request. $.post can be used for POST requests. $.get $.post

$.load can be used if you just need to insert something into the DOM: $.load
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to AJAX JQuery nested call
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25364 seconds with 12 queries