-2

I am using jQuery and I have to do it like this:

data = $.getJSON('/accounts/ajax/user_details/', {'user_id' : "$user_id"})
        .fail();
update_user_details(data);

... since if I set the callback function .done() as suggested in the official documentation, the variable 'data' seems not to be defined:

$.getJSON('/accounts/ajax/user_details/', {'user_id' : "$user_id"})
        .done( update_user_details(data) ),
        .fail();

Any ideas?

nsx
  • 697
  • 1
  • 13
  • 41
  • 1
    visit [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Satpal Nov 30 '13 at 08:02

1 Answers1

0

You need to pass a function reference (or an anonymous function) to .done(). Now you're passing the return value of the function. And there's an extra comma at the end of the .done() call.

$.getJSON('/accounts/ajax/user_details/', {'user_id' : "$user_id"})
    .done( update_user_details )    // <-- no parentheses, no comma
    .fail();

And, as a side note, .fail() by itself doesn't do anything; you have to pass it a function that handles the error.

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • Ok, that works. However, I think that my problem is that, in accordance to the documentation (http://api.jquery.com/jQuery.getJSON/), I thought that I had to do it as I wrote in my question. – nsx Nov 30 '13 at 09:16
  • 2
    The documentation uses anonymous functions correctly everywhere, so I'm not sure where you picked up the syntax. – JJJ Nov 30 '13 at 09:18