2

我正在尝试在叠加层中呈现一个表单。表单是通过 ajax 加载的。这一切都在 Firefox 3.x 上运行良好。

但在 Safari 5.0(5533.16) 上,覆盖显示但不发出 ajax 请求,而是发出 html 请求。

这是我在 js 中配置它的方法,然后在 document.ready 上调用 ModalPostForm.init();

馅饼 - http://pastie.org/1196064

var ModalPostForm = {
    init: function(){
        $("a[rel='#post_overlay']").each(function(){
            //alert($(this).attr('href'));
            $(this).overlay({
                closeOnClick: false,
                target: '#post_overlay',
                fixed: false,
                onBeforeLoad: function() {
                    // grab wrapper element inside content
                    var wrap = this.getOverlay().find(".contentWrap");
                    // load the page specified in the trigger
                    wrap.load(this.getTrigger().attr("href"));
                },
                onLoad: function() {
                    setTimeout("configurePostForm(200, 1000, 150)", 500);
                },
                onClose: function() {
                    $('.contentWrap').empty();
                }
            });
        });
    },

    close: function(){
        $("a[rel='#post_overlay']").each(function(){
            var ol = $(this).data("overlay");
            if(ol.isOpened()){
                ol.close();
            }
        });
    }
};

这是我的html

<div id="post_overlay" class="new-post-overlay span-12" style="top: 54.3px; left: 429px; position: absolute; display: none;"><a class="close"></a>
  <div style="display: none;" id="form_loading" class="">
    <div class="loading-inner-center"></div>
  </div>
  <div id="" class="contentWrap"></div>
</div>

为什么发出 html 请求?

谢谢

更新

如果我在我的控制器操作中删除我的 rails respond_to 块中的 format.html 块,这就会消失。

虽然问题仍然是为什么它发送一个 html 请求或者为什么 rails 认为它​​是一个 html 请求。对于确实不能做js的浏览器,我宁愿不删除那个块。

导致问题的新动作

        respond_to do |format|
#            format.html # new.html.erb
            format.js {

            }
#            format.xml  { render :xml => @post }
        end

也导致问题的编辑操作,新和编辑恰好是“获取”方法。

#            format.html {
#                if @edit
#                    #
#                    setup_post_items(true)
#                else
#                    flash[:error] = 'Sorry no further edits, Post has already been approved.'
#                    redirect_back_or_default root_url
#                end
#            }
            format.js {
                if !@edit
                    flash[:error] = 'Sorry no further edits, Post has already been approved.'
                end
            }
4

1 回答 1

2

我还注意到 format.html 块有时会捕获 js 请求。我用过两种方法来解决这个问题(以下任何一种都可以解决,你不需要同时做这两种方法):

a) 在您的 javascript 中,将“.js”附加到您的目标 href。例如:

if ( ! /\.js/.test($this.attr('href')) ) {
  $this.attr('href', function(index, href) {
    return href.replace(/([^\?]+)(.*)/, '$1.js$2');
  });
}

b) 在您的控制器中,将 format.js 块放在 format.html 块之前,以便 javascript 请求在匹配 html 块之前匹配 js 块。

于 2010-11-05T18:56:30.300 回答