-1

I'm using this jstree plugin And here's a simple question: how do I return something so the plugin can populate it's dom even if ajax fails?

EDIT: Cause success callback receives the response object(then it might mess with it), and then return. I wanted the error callback to be able to "modify the nothing" and then return as well.

It's simple! Or i'm so far away from the right concept that makes it complicated.

Patrick Bassut
  • 3,310
  • 5
  • 31
  • 54
  • 1
    http://api.jquery.com/jquery.ajax - It has a `success` callback, and an `error` callback – nbrooks Apr 07 '13 at 21:49
  • @nbrooks sorry, you did not get what i'm trying to do. As long as I know error callback does not return data(cause it failed). – Patrick Bassut Apr 07 '13 at 23:59
  • An ajax method can't 'return' anything, it's asynchronous. See [this post](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function) on jQuery ajax to figure out the set up. – nbrooks Apr 08 '13 at 05:59
  • I'll read the post. But, since I got the feeling you didn't understand what I said, I'll try to explain again. See the edit in a few minutes. – Patrick Bassut Apr 08 '13 at 12:53
  • The edit is not really helping. You seem to believe that the error callback function can't return anything, but that's not the case. What should your function do, exactly? – Aioros Apr 08 '13 at 13:00
  • Maybe you could post your existing code that works on Ajax success? That way, it would be a lot easier to explain how to modify it to also work on Ajax failure. – apsillers Apr 08 '13 at 13:10
  • @nbrooks The OP likely isn't using `$.ajax`; it appears that jsTree abstracts away the Ajax fetch. From the [jsTree docs](http://www.jstree.com/documentation/json_data): "The `error` and `success` functions (if present) also fire in the context of the tree, and **if you return a value in the `success` function it will be used to populate the tree**..." It appears that jsTree might not expose the necessary functionality to alter the tree from within the jsTree Ajax `error` callback. – apsillers Apr 08 '13 at 13:14
  • @nbrooks I'm not using $.ajax. That's why i mentioned the jstree. Otherwise, why mention? Sorry, didn't occurred to me you were i was using $.ajax. – Patrick Bassut Apr 08 '13 at 13:17
  • @apsillers that's exactly what i'm trying to to. – Patrick Bassut Apr 08 '13 at 13:18

1 Answers1

0

Try complete function

http://api.jquery.com/jQuery.ajax/ (find Complete on this page)

complete Type: Function( jqXHR jqXHR, String textStatus ) A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42