5

非常奇怪的问题:我有一个由两部分组成的下拉菜单,在其中选择一个州将添加第二个下拉菜单,为您提供该州的 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;
    }
  );
})

注意 - 此代码改编自此处的教程: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>

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"]
4

5 回答 5

12

所以我强行解决了这个问题。我真的不明白为什么这个问题是特定于 Firefox 的,但可能会调查它。

我可以通过为 dataType(get 方法的最后一个参数)添加一个参数来明确地将其声明为 html 来解决此问题。

Get 在此处的 JQuery 文档中进行了描述:http: //api.jquery.com/jQuery.get/

jQuery.get( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

因此,有效的代码是添加“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;
    }
  );
})

同样,我需要调查为什么这是 Firefox 特有的;这让我发疯了,所以希望它可以帮助别人。

于 2012-07-11T22:20:39.087 回答
0

不确定这是否只是不完整的复制粘贴,但

<select id="area_msa" name="area[msa]"><option value="">Select Area (Optional)</option>

需要关闭选择标签,否则你的 html 的其余部分嵌套在选择元素中......这很糟糕。

于 2012-07-11T20:42:17.643 回答
0

我只在 FF 中遇到了同样的问题。我的 Servlet 正在返回内容类型为“文本”的文本。FF 将此解释为 text/xml 并尝试在正文中插入 xml - 因此出现错误

我将内容类型更改为 text/plain - 一切都很好

于 2012-10-02T19:15:42.623 回答
0

我已经解决了这个添加

header('Content-Type: text/html; charset=utf-8');

在 php 中获取文件。当标头未正确设置时,会发生错误(当从一台服务器移动到另一台服务器时,在旧服务器上它工作正常,在新服务器上工作正常,直到我设置标头)

于 2014-03-25T14:50:27.420 回答
0

我遇到了同样的问题,并且仅在响应为空时发生,例如,我期待一个包含记录的表,但没有任何内容可显示。

于 2014-09-02T10:57:55.950 回答