Rails 抛出“REXML::ParseException 没有有效的根”例外

发布于 2024-11-09 00:00:10 字数 412 浏览 0 评论 0原文

我有一个 JavaScript 对象,并使用 Douglas Crockford 的 JSON 实用程序< /a>.在发送 AJAX 后请求时,我得到:

REXML::ParseException 没有有效的根

REXML::ParseException (The document "{\"name\":\"asda\",\"deadline\":\"May 24, 2011\"}" does not have a valid root):

我无法继续处理此错误。

I have a JavaScript object, and converted it into JSON using Douglas Crockford's JSON utility. While sending a post AJAX request, I get:

REXML::ParseException does not have a valid root

REXML::ParseException (The document "{\"name\":\"asda\",\"deadline\":\"May 24, 2011\"}" does not have a valid root):

I am not able to proceed with this error.

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

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

发布评论

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

评论(3

ゝ偶尔ゞ 2024-11-16 00:00:10

当发出 AJAX 请求时,您可能发送了错误的 Content-Type 标头。如果您发出带有 Content-Type: application/xml 标头的请求,Rails 会尝试将您的请求正文解析为 XML,并且您将收到该错误消息。相反,您需要使用 application/json 或其他内容类型,以便 Rails 不会尝试解析它。如果您使用的是 jQuery,则这是 $.ajax< 中的 contentType 设置/a>.

When making your AJAX request, you're probably sending the wrong Content-Type header. If you make a request with the header Content-Type: application/xml, Rails will try to parse your request body as XML, and you'll get that error message. Instead, you'll want to use application/json or another content-type so that Rails won't try to parse it. If you're using jQuery, this is the contentType setting in $.ajax.

荒路情人 2024-11-16 00:00:10

JSON 不像 XML 那样要求有根元素。尝试使用 JSON.parse(json_string) 解析它,而不是使用 REXML。

JSON doesn't require there to be a root element the way XML does. Try parsing it with JSON.parse(json_string), instead of with REXML.

城歌 2024-11-16 00:00:10

请参阅下面提到的 json 文件,

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

这里的词汇表是根元素,它包含整个 json,类似于下面显示的 xml。

<!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN">
 <glossary><title>example glossary</title>
  <GlossDiv><title>S</title>
   <GlossList>
    <GlossEntry ID="SGML" SortAs="SGML">
     <GlossTerm>Standard Generalized Markup Language</GlossTerm>
     <Acronym>SGML</Acronym>
     <Abbrev>ISO 8879:1986</Abbrev>
     <GlossDef>
      <para>A meta-markup language, used to create markup
languages such as DocBook.</para>
      <GlossSeeAlso OtherTerm="GML">
      <GlossSeeAlso OtherTerm="XML">
     </GlossDef>
     <GlossSee OtherTerm="markup">
    </GlossEntry>
   </GlossList>
  </GlossDiv>
 </glossary>

因此我们可以说词汇表是根,并且只是 xml 或 json 中的一个标签。

因此,您必须在 json 文件中提供一个包含整个 JSON 文件的根元素。

See the below mentioned json file

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

Here glossary is the root element which encloses the whole json similarly to xml as displayed below.

<!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN">
 <glossary><title>example glossary</title>
  <GlossDiv><title>S</title>
   <GlossList>
    <GlossEntry ID="SGML" SortAs="SGML">
     <GlossTerm>Standard Generalized Markup Language</GlossTerm>
     <Acronym>SGML</Acronym>
     <Abbrev>ISO 8879:1986</Abbrev>
     <GlossDef>
      <para>A meta-markup language, used to create markup
languages such as DocBook.</para>
      <GlossSeeAlso OtherTerm="GML">
      <GlossSeeAlso OtherTerm="XML">
     </GlossDef>
     <GlossSee OtherTerm="markup">
    </GlossEntry>
   </GlossList>
  </GlossDiv>
 </glossary>

Hence we can say that the glossary is the root and is only one tag in xml or json.

Hence you have to provide a root element in the json file which encloses the whole JSON file.

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