views:

351

answers:

2

Within my company's online application, we've set up a JSONP-based API that returns some data that is used by a bookmarklet I'm developing. Here is a quick test page I set up that hits the API URL using jQuery's $.ajax() method:

http://troy.onespot.com/static/3915/index.html

If you look at the requests using Firebug's "Net" tab (or the like), you'll see that what's happening is that the URL is requested successfully, but since our app redirects any unauthorized users to a login page, the login page is also requested by the browser and seemingly interpreted as JavaScript. This inevitably causes an exception since the login page is HTML, not JavaScript.

Basically, I'm looking for any sort of hook to determine when the request results in a redirect - some way to determine if the URL resolved to a JSONP response (which will execute a method I've predefined in the bookmarklet script) or if it resulted in a redirect. I tried wrapping the $.ajax() method in a try {} catch(e) {} block, but that doesn't trap the exception, I'm assuming because the requests were successful, just not the parsing of the login page as JavaScript. Is there anywhere I could use a try {} catch(e) {} block, or any property of $.ajax() that might allow me to hone in on the exception or otherwise determine that I've been redirected?

I actually doubt this is possible, since $.getScript() (or the equivalent setup of $.ajax()) just loads a script dynamically, and can't inspect the response headers since it's cross-domain and not truly AJAX:

http://api.jquery.com/jQuery.getScript/

My alternative would be to just fire off the $.ajax() for a period of time until I either get the JSONP callback or don't, and in the latter case, assume the user is not logged in and prompt them to do so. I don't like that method, though, since it would result in a lot of unnecessary requests to the app server, and would also pile up the JavaScript exceptions in the meantime.

Thanks for any suggestions!

+1  A: 

When using the AJAX methods within jQuery, it should be setting a header of 'X-Requested-By: XMLHttpRequest' in the request.

My initial thinking would be that you could check and see if that header is set on the server side, and if so, instead of issuing a redirect, send back some JSON with a redirection URL, and have the client JS look for the presence of that redirection in the response, with the server only issuing the redirect when it's not an AJAXy call.

Brian Arnold
Yes, something like http://stackoverflow.com/questions/1462919/form-that-makes-browser-redirect-when-accessed-by-either-a-regular-form-submit-or/1463206#1463206
Crescent Fresh
Thanks, Brian - $.getScript() or the $.ajax() long-form equivalent aren't actually AJAX requests (please see my comment above). However, you might be on to something with the server-side check. Maybe if the API URL receives a request from a user who is not logged in, rather than redirecting them to the login page, it could just return a JSONP response indicating an unauthorized user. Then my bookmarklet could pass that info along to the user. I'll have to check with our Rails guys to see if this is feasible. Thanks again!
Bungle
A: 

According to the jQuery documentation if you don't specify dataType as script, as you are doing now, it'll try to figure out what the response is based on the MIME type. You could then also add an option called dataFilter to the $.ajax method; it is a function which takes two parameters, dataFilter(data, type) and type is the data type as determined by jQuery, you can look at the type and procede accordingly.

Adam
Thanks, Adam - however, I don't think this will work in my situation - please note my comments above.
Bungle