【问题标题】:$.getJSON interpreted differently by IE - Ruby on RailsIE 对 $.getJSON 的解释不同 - Ruby on Rails
【发布时间】:2010-11-09 14:56:16
【问题描述】:

我正在尝试做经典类别 -> 子类别链接下拉列表(在类别中选择某些内容,填充子类别)。

我的代码适用于除 IE 之外的所有浏览器(当然)。

这是我正在使用的 JS 代码:

$("body select#category").data_binding({
    child: "select#company_subcategory_id",
    url: "subcategories",
});

   [ . . . ]

    data_binding: function(options) 
    {
            $(this).change(function()
            {
                    $.getJSON("/"+ options.url +"/",
                             { id: $(this).val(), ajax: 'true'}, 
                              function(j)
                              { 
                                    for (var i = 0; i < j.length; i++) 
                                    {
                                       options += '<option value="' + j[i].optionValue + '">';
                                       options += j[i].optionDisplay;
                                       options += '</option>';
                                    }

                                    $(child).html(options);
                              });
            });
   }

子类别控制器

class SubcategoriesController < ApplicationController
  layout 'application'

  def index
       @subcategories = Subcategory.find_all_by_category_id(params[:id])
       respond_to do |format|
           format.js {render :json => @subcategories.collect {|sc| {:optionValue => sc.id,
                                                           :optionDisplay => sc.name} }.to_json }
          end
  end

  def show  
      @subcategory = Subcategory.category_permalink_like(params[:category]).
                                             permalink_like(params[:subcategory]).first
      @with_banner = @subcategory.companies.active.with_banner
      @without_banner = @subcategory.companies.active.without_banner
   end
end

我正在跟踪 development.log 文件,当我使用除 IE 之外的任何浏览器时,日志都会显示

Processing SubcategoriesController#show (for 192.168.1.70 at 2010-08-26 01:49:06) [GET]
Parameters: {"id"=>"4", "_"=>"1282805337516", "show_type"=>"available_banners"}

但是当我使用 IE 时,我得到了

Processing SubcategoriesController#create (for 192.168.1.70 at 2010-08-26 01:50:09) [POST]
Parameters: {"ajax"=>"true", "authenticity_token"=>"Eg2XAvSSHg/v12cKjTPt+HkKWhxdGW3s5n6lm9jHu2A=", "id"=>"6"}

没有定义创建操作,所以它崩溃了。

我不知道为什么会有不同的解释:/

有什么建议吗?

谢谢!!

【问题讨论】:

  • SubcategoriesController#create 是什么样的?
  • 没有 SubcategoriesController#create 根本不应该被调用:/

标签: jquery ruby-on-rails internet-explorer drop-down-menu getjson


【解决方案1】:

好的,这是一个荒谬的解决问题的方法,但我束手无策,这似乎可行。

我像这样修改了子类别控制器:

class SubcategoriesController < ApplicationController
  layout 'application'

  def index
   @subcategories = Subcategory.find_all_by_category_id(params[:id])
   respond_to do |format|
       format.js {render :json => @subcategories.collect {|sc| {:optionValue => sc.id,
                                                       :optionDisplay => sc.name} }.to_json }
      end
  end

  def show  
      @subcategory = Subcategory.category_permalink_like(params[:category]).
                                         permalink_like(params[:subcategory]).first
      @with_banner = @subcategory.companies.active.with_banner
      @without_banner = @subcategory.companies.active.without_banner
   end

   #this is the added method
   def create
      index
   end
end

由于某种原因,IE 不断尝试在所有其他浏览器都运行良好的子类别控制器中调用 create 方法并调用 index 方法

我真的不知道为什么会发生这种情况,所以这个黑客现在必须做:(

我已经简化了上面的代码,使其更易于阅读。

【讨论】:

    【解决方案2】:

    我记得最近调试过一个与此非常相似的问题。如果我没记错的话,它与传递给 jquery 的 mime 类型和数据类型有关。看看改用 get 或 getScript。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多