Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails throws "REXML::ParseException does not have a valid root" exception

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.

like image 460
felix Avatar asked Feb 24 '23 02:02

felix


1 Answers

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.

like image 193
Jonathan Tran Avatar answered Feb 26 '23 23:02

Jonathan Tran