8月 152014
 

问题

为什么jQuery.ajax() 没有默认支持302重定向?

解答

jQuery.ajax 是通过xmlHttpRequest来实现的,而这个东西存在跨域问题,如果302跳转到的地址还是当前域,则服务器端几乎没有302重定向的必要;如果302跳转到的地址不是当前所在的域,则必然存在跨域问题。可能就是这个原因吧。

但是:

出于跨域资源共享的需要,很多浏览器开始支持cors,如果浏览器支持了cors(假设server端也做了相应的配置),则,jQuery.ajax似乎应该支持302才是,如果自己实现,请注意,如果需要上行cookie(一般是需要的),需要设置  xhr.withCredentials=true

 

如何实现302

对于jQuery.ajax来讲,302是一个错误,可以通过实现 ajaxComplete 事件来完成,具体参考: http://api.jquery.com

参考文章:

http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call

http://stackoverflow.com/questions/6955308/in-an-ajax-call-302-is-not-followed

http://www.cnblogs.com/Darren_code/p/cors.html

http://blog.csdn.net/hfahe/article/details/7730944

 

 Posted by at 下午 2:23

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据