开发者

JQuery get/ajax call executes callback without waiting for response

开发者 https://www.devze.com 2022-12-30 03:40 出处:网络
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开发者_StackOverflow anything

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开发者_StackOverflow anything I'm missing? Thanks in advance.

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

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


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


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.


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/)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号