使用jQuery的$ .ajax()或$ .getScript()方法时,有什么方法可以识别重定向? [英] Any way to identify a redirect when using jQuery's $.ajax() or $.getScript() methods?

查看:104
本文介绍了使用jQuery的$ .ajax()或$ .getScript()方法时,有什么方法可以识别重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我公司的在线应用程序中,我们建立了一个基于JSONP的API,该API返回一些我正在开发的书签使用的数据.这是我设置的快速测试页面,该页面使用jQuery的$.ajax()方法访问API URL:

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

如果您使用Firebug的网络"标签(或类似标签)查看请求,则会看到发生的情况是成功请求了URL,但是由于我们的应用将所有未经授权的用户重定向到登录页面,因此浏览器也请求登录页面,并且看似解释为JavaScript.由于登录页面是HTML,而不是JavaScript,因此不可避免地会导致异常.

基本上,我正在寻找某种挂钩来确定请求何时导致重定向-一种确定URL是否解析为JSONP响应的方法(它将执行我在bookmarklet脚本中预定义的方法),或者是否导致重定向.我尝试将$.ajax()方法包装在try {} catch(e) {}块中,但这并没有捕获异常,我假设是因为请求成功,但不是将登录页面解析为JavaScript.

有什么地方可以使用try {} catch(e) {}块,或者可以使用$.ajax()的任何属性来应对异常,或者确定我是否已被重定向?

我实际上怀疑这是否可行,因为$.getScript()(或$.ajax()的等效设置)只是动态加载脚本,并且由于它是跨域的而不是真正的AJAX,因此无法检查响应头:

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

我的替代方法是将$.ajax()触发一段时间,直到我收到JSONP回调或没有收到JSONP回调为止,在后一种情况下,假定用户未登录并提示他们进行操作所以.不过,我不喜欢这种方法,因为它会导致对应用服务器的许多不必要的请求,同时还会堆积JavaScript异常.

谢谢您的建议!

解决方案

在jQuery中使用AJAX方法时,应在请求中设置标头"X-Requested-By:XMLHttpRequest".

我最初的想法是,您可以检查并查看是否在服务器端设置了该标头,如果这样,则不发出重定向,而是发送带有重定向URL的JSON并让客户端JS查找响应中存在该重定向,服务器仅在不是AJAXy调用时才发出重定向.

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!

解决方案

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.

这篇关于使用jQuery的$ .ajax()或$ .getScript()方法时,有什么方法可以识别重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆