Ajax请求永远是304的解决方案

我之前写了一个NavigationService,用来做页面的Ajax导航,但是今天发现一个问题,在IE下面,发现Ajax的请求不会真正的被发送到服务器端,在IE里面,返回的永远是304。这个应该是IE的设计问题:查了一下,发现这个博文里面提到了问题的本质:http://blog.sina.com.cn/s/blog_4b7809800100y1c3.html“因为ajax请求的时候如果使用get方式请求,同时路径参数相同的时候,ajax会先从本地缓存中取,如果取到了它是不会去请求后台的”,知道了问题就好办了,照着文章中写的那样,给每个AJAX的请求都加一个请求参数,以保证URL永远不同。这个是修改之后的代码:

# require: jQuery
# Encapsulate the logic to dynamic load content by Url flag segment

class UrlFlagNaviListener
#public:
    constructor: (@m_container, @m_uiLoading, @m_uiError)->
        this.__addListener();

    naviContent: (contentAjaxUrl)->
        contentAjaxUrl = (contentAjaxUrl || window.location.hash).slice(1);
        this._naviContentImpl(contentAjaxUrl);

#protected:
    _naviContentImpl: (contentAjaxUrl) -> # to be overriden

#private:
        
    __addListener: ()->
        $(window).on("hashchange", ()=>
            this.naviContent();
        );


class ContentNaviListener extends UrlFlagNaviListener
#protected:
    _naviContentImpl: (contentAjaxUrl) ->
        if (contentAjaxUrl isnt '')
            # cancel the previous page's deferred
            if (@m_prevRequest?.deferred.state() is 'pending')
                console.log("Previous request is cancelled.")
                @m_prevRequest.deferred.abort();
                @m_prevRequest.stopAnimation();

            @m_prevRequest = null;
            @m_container.hide();
            @m_uiError.hide();

            request = {};

            # Start animation and return the function to stop it
            request.stopAnimation = (()=>
                animation = ()=>@m_uiLoading.fadeIn(800).fadeOut(1000);
                animation();
                timer = setInterval(animation, 2000);
                stopAnimation = ()=>
                    clearInterval(timer);
                    @m_uiLoading.stop().hide();
                return stopAnimation
            )();
            
            # Force IE9 refresh the ajax page
            leadingChar = if (contentAjaxUrl.indexOf("?") is -1) then "?" else "&"
            contentAjaxUrl += "#{leadingChar}timeStamp=#{new Date().getTime()}"

            request.deferred = $
                .get(contentAjaxUrl)
                .done( (data, textStatus, jqXHR)=>
                    request.stopAnimation();
                    @m_container.show().html(data) )
                .fail( ()=>
                    request.stopAnimation();
                    @m_uiError.show() )

            @m_prevRequest = request;

############
# Exports
############
window.ContentNaviListener = ContentNaviListener;






转载于:https://www.cnblogs.com/puncha/p/3876858.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值