仅在Firefox中的JQuery AJAX异常:"节点无法插入层次结构中的指定点"(HierarchyRequestError)

Dav*_*ino 5 ajax jquery ruby-on-rails ruby-on-rails-plugins

非常奇怪的问题:我有一个2部分的下拉列表,选择一个州将添加第二个下拉列表,为您提供该州的MSA区域列表.

这是通过对控制器的JQuery Get请求来完成的,该控制器返回Select下拉列表中的区域列表,例如

jQuery(function($) {
  // when the #area_state field changes
  $("#area_state").change(
    function() {
      // make a call and replace the content
      var state = $('select#area_state :selected').val();
      if(state == "") state="0";
      jQuery.get(
        '/getmsas/' + state,
        function(data){ $("#msas").html(data); }
      )
    return false;
    }
  );
})
Run Code Online (Sandbox Code Playgroud)

注 - 此代码改编自此处的教程:http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/

这在Chrome和IE中运行良好,但在Firefox(13.0.1)中它不起作用,产生两个错误:

Error: junk after document element
Source File: http://localhost:3000/getmsas/Connecticut
Line: 2, Column: 1
Source Code:
<select id="area_msa" name="area[msa]"><option value="">Select Area (Optional)</option>
Run Code Online (Sandbox Code Playgroud)

Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point
in the hierarchy"  code: "3" nsresult: "0x80530003 (HierarchyRequestError)"  location:   
"http://localhost:3000/assets/jquery.js?body=1 Line: 6498"]
Run Code Online (Sandbox Code Playgroud)

Dav*_*ino 12

所以我粗暴地强迫解决这个问题.我真的不明白为什么这个问题特定于Firefox,但可能会对其进行调查.

我能够通过为dataType(get方法的最后一个参数)添加一个参数来明确地将其声明为html来解决这个问题.

Get在JQuery文档中描述:http://api.jquery.com/jQuery.get/

jQuery.get( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
Run Code Online (Sandbox Code Playgroud)

所以有效的代码是添加"html"作为dataType参数:

jQuery(function($) {
  // when the #area_state field changes
  $("#area_state").change(
    function() {
      // make a call and replace the content
      var state = $('select#area_state :selected').val();
      if(state == "") state="0";
      jQuery.get(
        '/getmsas/' + state,
        function(data){ $("#msas").html(data); },
        "html"
        // ABOVE LINE IS THE FIX
      )
    return false;
    }
  );
})
Run Code Online (Sandbox Code Playgroud)

同样,我需要调查为什么这是特定于Firefox的; 这让我发疯,所以希望能帮到别人.