如何克服"total_rows"问题从CouchDB解析JSON时 [英] How to get past the "total_rows" when parsing JSON from CouchDB

查看:122
本文介绍了如何克服"total_rows"问题从CouchDB解析JSON时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下jQuery代码从CouchDB获取JSON文件.

I'm using the following jQuery code to get a JSON file from CouchDB.

Function getURL() {
   var api_url = 'http://127.0.0.1:5984/couchcontentqueue/_design/DocCollections/_view/view_all_by_url_name?key="favorite-flickr-photos"&?callback=?';

   $.getJSON(api_url, function(json) {
      var type = json.type;
      var desc = json.description;
      $("#dropBox h3").html(type);
      $("#dropBox p").html(desc);
   });
};

当我对该URL执行GET时,它会提供以下内容:

When I do a GET on that URL it provides back the following:

{"total_rows":6,"offset":5,"rows":[ {"id":"f5ba37e5af406ab079d596f7a1f30a2d","key":.... ]}

{"total_rows":6,"offset":5,"rows":[ {"id":"f5ba37e5af406ab079d596f7a1f30a2d","key":.... ]}

Firebug给我以下错误: 标签无效 http://127.0.0.1:5984/couchcontentqueue/_design/DocCollections/_view/view_all_by_url_name?key=%22favorite-flickr-photos%22&?callback=jsonp1304111285023 第1行

Firebug gives me the following error: invalid label http://127.0.0.1:5984/couchcontentqueue/_design/DocCollections/_view/view_all_by_url_name?key=%22favorite-flickr-photos%22&?callback=jsonp1304111285023 Line 1

我不知道如何越过第一行才能到达实际的JSON对象.有任何想法吗?谢谢.

I can't figure out how to get past that first line to get to the actual JSON object. Any ideas? Thanks.

推荐答案

?callback =

?callback=

您似乎正在尝试执行JSONP请求,但是:

It looks like you are trying to do a JSONP request, but:

{"total_rows":6,...

{"total_rows":6, ...

是简单的JSON响应,而不是JSONP调用.如果您不想执行跨域JSONP请求,请删除callback参数,并让jQuery将响应解析为普通JSON.

Is a plain JSON response and not a JSONP call. If you don't mean to do a cross-domain JSONP request, get rid of the callback parameter and have jQuery parse the response as normal JSON.

如果您要做需要执行跨域JSONP请求,并且您了解这样做的安全隐患,请确保您使用的是最新的CouchDB版本并添加指令:

If you do need to do cross-domain JSONP requests, and you understand the security risks of that, make sure you're using an up-to-date CouchDB version and add the directive:

allow_jsonp = true

[http]部分中的.ini文件.

to the .ini file in the [http] section.

无效标签

是您尝试执行/eval包含JSON对象的字符串时得到的结果. JS解析的一个古怪之处是,{"x": "foo"}中的"x"被当作语句块中的JavaScript标签"(很少用于continue语句),而不是对象文字表达式中的对象属性名称.

is what you get when you try to execute/eval a string containing a JSON object. It is a quirk of JS parsing that the "x" in {"x": "foo"} is taken as a JavaScript ‘label’ (used rarely for continue statements) in a statement block, rather than an object property name in an object literal expression.

当jQuery认为您正在执行JSONP请求时,它将使用脚本执行而不是JSON解析.您的网址中神奇地包含了"callback ="参数.

jQuery will use script execution instead of JSON parsing when it thinks you are doing a JSONP request. Having the ‘callback=’ parameter in your URL magically makes it think that.

这篇关于如何克服"total_rows"问题从CouchDB解析JSON时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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