Rails抛出“REXML :: ParseException没有有效的根”异常

时间:2022-05-15 04:23:35

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

我有一个JavaScript对象,并使用Douglas Crockford的JSON实用程序将其转换为JSON。在发送后AJAX请求时,我得到:

REXML::ParseException does not have a valid root

REXML :: ParseException没有有效的根

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.

我无法继续此错误。

3 个解决方案

#1


9  

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.

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

#2


0  

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.

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

#3


-1  

See the below mentioned json file

请参阅下面提到的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"
                }
            }
        }
    }
}

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

这里词汇表是根据xml包含整个json的根元素,如下所示。

<!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.

因此我们可以说术语表是根,并且只是xml或json中的一个标记。

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

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

#1


9  

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.

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

#2


0  

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.

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

#3


-1  

See the below mentioned json file

请参阅下面提到的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"
                }
            }
        }
    }
}

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

这里词汇表是根据xml包含整个json的根元素,如下所示。

<!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.

因此我们可以说术语表是根,并且只是xml或json中的一个标记。

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

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