MongoDB _id 字段上的 Jquery ajax 解析器错误
当访问从 MongoDB 文档返回 JSON 的请求时,我看到一个奇怪的解析器错误 (parsererror)。
该文档返回一个神秘的解析器错误:
{"data":{"first_name":"Ray","last_name":"Reinger","_id":4e9c0ed27763dfba37000001}}
该文档不返回错误:
{"data":{"first_name":"Ray","last_name":"Reinger"}}
正在使用的jquery是:
$("#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;
});
请求本身很好。 返回的 mime-type 是“application/json”。 所有字段和值都被引用。 据我所知,_id 是有效的 JSON。
基本上,打开和关闭 _id 就可以使事情正常进行。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在写上面的问题时,我看到了这个问题。在这里发布答案,供后代和其他可能遇到同样问题的人使用。
核心问题是 ID 值没有被引用。
我覆盖了 ID 的默认 ruby-mongo-driver 渲染(使用嵌套哈希),并且副作用是 id 字符串没有被引用:
注意 *to_s.to_json* 这正确地将 ObjectId 引用为 JSON 字符串。
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.