CouchDB getJSON 没有返回我所期望的
我在 iriscouch 上有一个测试帐户,
我正在尝试编写一个例程来处理返回的 JSON。
function getMyJson(url) {
$('#dispJson').html('<h3>Json Data from: ' + url);
data = $.getJSON(url);
$.each(data, function(key, val) {
$('#dispJson').append('key: ' + key + ' Val: ' + val + '<br />');
});
return true;
};
但我似乎得到了 JSON,但不是我所期望的
结果
key: readyState Val: 1
key: setRequestHeader Val: function ( name, value ) { if ( !state ) { var lname = name.toLowerCase(); name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name; requestHeaders[ name ] = value; } return this; }
key: getAllResponseHeaders Val: function () { return state === 2 ?
......
jQuery.type( elem ); if ( type === "array" ) { deferred.done.apply( deferred, elem ); } else if ( type === "function" ) { callbacks.push( elem ); } } if ( _fired ) { deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] ); } } return this; }
key: statusCode Val: function ( map ) { if ( map ) { var tmp; if ( state < 2 ) { for( tmp in map ) { statusCode[ tmp ] = [ statusCode[tmp], map[tmp] ]; } } else { tmp = map[ jqXHR.status ]; jqXHR.then( tmp, tmp ); } } return this; }
事实上,我从 IrisCouch 网站上使用的任何 url 都得到了该响应。
我已将 JSONP 设置为 true,并且还使用 ?callback=? 进行了测试附加到 URL。
我希望有人认识到我得到的输出,并能就我误解或做错的事情提出建议。
谢谢 mcl
I have a test account on iriscouch
I am trying to write a routine to process the JSON returned.
function getMyJson(url) {
$('#dispJson').html('<h3>Json Data from: ' + url);
data = $.getJSON(url);
$.each(data, function(key, val) {
$('#dispJson').append('key: ' + key + ' Val: ' + val + '<br />');
});
return true;
};
But I appear to be getting JSON back, but not what I was expecting
Results
key: readyState Val: 1
key: setRequestHeader Val: function ( name, value ) { if ( !state ) { var lname = name.toLowerCase(); name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name; requestHeaders[ name ] = value; } return this; }
key: getAllResponseHeaders Val: function () { return state === 2 ?
......
jQuery.type( elem ); if ( type === "array" ) { deferred.done.apply( deferred, elem ); } else if ( type === "function" ) { callbacks.push( elem ); } } if ( _fired ) { deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] ); } } return this; }
key: statusCode Val: function ( map ) { if ( map ) { var tmp; if ( state < 2 ) { for( tmp in map ) { statusCode[ tmp ] = [ statusCode[tmp], map[tmp] ]; } } else { tmp = map[ jqXHR.status ]; jqXHR.then( tmp, tmp ); } } return this; }
In fact I get that response from whatever url I use on the IrisCouch site.
I have JSONP set to true and I also tested with ?callback=? appended to the URL.
I am hoping someone recognises the output I am getting and can advise me as to what I have misunderstood or done wrong.
Thanks mcl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将
data
设置为 jQuery 返回的 XHR 对象。请记住,XHR 是异步的,因此您不能期望结果立即可用。相反,您应该这样做:
一定要通读文档:http://api. jquery.com/jQuery.getJSON/
You're setting
data
to the XHR object returned by jQuery. Remember that XHR is asynchronous so you can't expect the result to be immediately available.Instead, you should be doing something like this:
Be sure to read through the documentation: http://api.jquery.com/jQuery.getJSON/
非常感谢。现在它正在按我的预期工作。 dataType 确实需要指定为“jsonp”(我认为因为它是跨域访问),为了清楚起见,我添加了类型“get”。
只需要弄清楚如何将值识别为数组,然后处理它们。我想我最终可能会得到很多级别,并且某个地方有一个漂亮的分层树例程可以完成这一切。但是,嘿,我们必须从头开始学习。
再次感谢
Many thanks. It is now working as I expected. The dataType does need to be specified as 'jsonp' (I think because it is a cross domain access) and I added type 'get' for my clarity.
Just need to figure out how to identify Values as arrays and then process them. I imagine I could end up with many levels and somewhere there is a beautiful hierarchical tree routine that does it all. But hey, we have to learn it from scratch.
Thanks again