Rails抛出“REXML :: ParseException没有有效的根”例外 [英] Rails throws "REXML::ParseException does not have a valid root" exception

查看:65
本文介绍了Rails抛出“REXML :: ParseException没有有效的根”例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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没有有效的根

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.

推荐答案

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

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.

这篇关于Rails抛出“REXML :: ParseException没有有效的根”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆