开发者

Any way to identify a redirect when using jQuery's $.ajax() or $.getScript() methods?

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 th开发者_运维问答e 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜