I am seeing a strange parser error (parsererror) when accessing a request returning JSON from a MongoDB document.
This document returns a cryptic parsererror:
{"data":{"first_name":"Ray","last_name":"Reinger","_id":4e9c0ed27763dfba37000001}}
This document does not return the error:
{"data":{"first_name":"Ray","last_name":"Reinger"}}
The jquery being used is:
$("#fetch").click(function(){
var url = "http://localhost:3333/people/4e9c0ed27763dfba37000001";
$.ajax({
url: url,
method: 'GET',
success: function(data, status){
//do a thing with the data
},
complete:function(jqXHR, status) {
console.log(status) //displays 'parsererror'
}
});
return false;
});
Request itself is fine.
Returned mime-type is ‘application/json’.
All the fields and values are quoted.
_id is valid JSON as far as I can grok.
Basically turning the _id on and off makes things work.
In writing the above question, I saw the problem. Posting the answer here for posterity and for anyone else who may run into the same issue.
The core problem is that the ID value was not being quoted.
I was overiding the default ruby-mongo-driver rendering of the ID (which uses a nested hash) and as a side-effect the id string was not being quoted:
Note the use of *to_s.to_json* this properly quotes the ObjectId as a JSON string.