views:

222

answers:

3

I'm having issues with my JQuery Ajax Call. The response is always null because the callback function is executed before the request to get_friends is completed. Is there anything I'm missing? Thanks in advance.

jQuery(document).ready(function($) {
   $('#btn_update').click(function() {

       url = '/get_friends/';
       $.get(url, {}, function(response){
            alert(response);
       });
   });
});
A: 

I don't see anything there that would explain the behavior. In fact, I copied and pasted that code and just changed the URL, and got the expected result (it waited for the call to complete before showing the response from the call). So no, you're not missing anything in that code, the problem must lie elsewhere.

OT (perhaps): Are you declaring url as a variable in the scope containing this code? Because otherwise, you're creating an implicit global variable, which is a Bad Thing(tm). :-) But that wouldn't explain the behavior you're describing.

T.J. Crowder
A: 

My coworker was able to help me out. I had to add "return false;" at the end of the click function.

dannyroa
Ah...it was a *submit* button.
T.J. Crowder
A: 

could you specify the last parameter of $.get. It can be a value of dataType of $.ajax. Try with "text" and see what you really receive from server. Then you can change to the type which you really need (see http://api.jquery.com/jQuery.ajax/)

Oleg