IE 对 $.getJSON 的解释不同 - Ruby on Rails

发布于 2024-09-15 21:50:11 字数 2470 浏览 6 评论 0原文

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

我的代码适用于除 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"}

There is no Defined create action, so it crashes。

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

有什么建议吗?

谢谢!!

I am trying to do the classic category -> subcategory chained dropdowns (where selecting something in category, populates the subcategory).

The code I have works in all browsers except IE (naturally).

Here is the JS code I am using:

$("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);
                              });
            });
   }

Subcategories controller

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

I'm tailing the development.log file and when I use any browser except IE the log shows

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

but when I use IE I get

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"}

There is no defined create action, so it crashes.

I have no idea why this is interpreted differently :/

Any suggestions?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洋洋洒洒 2024-09-22 21:50:11

好吧,对于这个问题来说,这是一个荒谬的解决方案,但我已无计可施,这似乎有效。

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

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 方法

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

我已经简化了上面提出的代码,以便更容易阅读。

Ok, this is a ridiculous fix for the problem, but I am at my wits end and this seems to work.

I modified the subcategories controller like so:

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

for some reason, IE keeps trying to call the create method in the subcategories controller where all the other browsers play nice and call the index method

I really don't know why this is happening, so this hack will have to do for now :(

I've simplified the code I posed above so its easier to read.

没有心的人 2024-09-22 21:50:11

我记得最近调试了一个与此非常相似的问题。如果我没记错的话,这与哑剧类型和类型有关。传递给 jquery 的数据类型。看看使用 get 或 getScript 代替。

I recall debugging an issue very similar to this recently. If I recall correctly, it has to do with the mime type & dataType passed to jquery. Look at using get, or getScript instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文