Meaningless Drivel is fun!*
The moose likes HTML, CSS and JavaScript and the fly likes Call a function before Ajax success Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Call a function before Ajax success" Watch "Call a function before Ajax success" New topic
Author

Call a function before Ajax success

Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hi all,

I have a bunch of AJAX calls in my site, which on success does DOM modifications. But before invoking these success functions, i would want to intercept the call with a function to inspect the response and certain other things ( from server ) before performing DOM changes. This callback needs to be applied for all the AJAX calls from the client.




I tried doing something like . But this would execute function2 irrespective of function1's return. I could add a reference of function1 from inside function2 but that's something i dont like because function2 (which intends to do DOM changes) need not know about function1.Is there a way to chain the functions and execute them based on the return type?

Thanks
Srikkanth
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

No. You'll need to direct the activities from the single success callback. (Which to me makes the most sense anyways -- doing it any other way just adds needless complexity.)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Thanks Bear, I would want to check something like




Do you think $.parseJSON would work in this scenario and in the major browsers ( IE , FF ) ?

Thanks
Srikkanth
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

As far as I know, $.parseJSON works fine in all browsers using the latest version of jQuery. There were some problems in earlier versions.
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Thanks Bear, on a related note, I'm trying to add some error handlers

For HTTP 4x codes, but it's weird the jqXHR's status is updated for 404 error but not for 401, the status is 0 but the statusText is 'Unauthorized'.. same is the case for 302, 'Authentication required'. I thought i could write a function like

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

I don't have a setup to test at the moment. Is the error callback not being invoked for a 401? (A 3xx is not an error, so that's not surprising.)

You might try the complete callback.
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
The error callback does gets invoked for 401, but jqXHR.status is not updated ( the value is zero , i was checking with firebug) but the statusText property of jqXHR is updated to 'Unauthorized'. I guess jquery translates the 401 to Unauthorized , but i'm not sure what are all the possible built-in errors which i could use and i couldnt find it jqXHR documentation. Any pointers?

Thanks for your help again
Sri
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Srikkanth Mohanasundaram wrote:I guess jquery translates the 401 to Unauthorized

Well that is exactly what a 401 means. Why the xhr status isn't set, I'm not sure.

i couldnt find it jqXHR documentation. Any pointers?

http://api.jquery.com/Types/#jqXHR
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Yes i understand , but there is no documentation on where this translated text is coming from, not sure if this is a cleaner way to check in the code.



The jqXHR documentation doesnt have all the possible values for textStatus, and it doesnt set the status which is weird.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call a function before Ajax success