2

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);
   });
};

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

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

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

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

mapmalith
  • 1,303
  • 21
  • 38
tabdon
  • 157
  • 14
  • Can you post a bit more of the JSON and state which specific properties you'd like to access? – Kevin Ennis Apr 29 '11 at 21:22
  • This particular JSON file has pointers to other documents. I'd like to be able to access the key as well as all items in the doc. {"total_rows":6,"offset":5,"rows":[ {"id":"f5ba37e5af406ab079d596f7a1f30a2d","key":"favorite-flickr-photos","value":{"_id":"f5ba37e5af406ab079d596f7a1f30a2d","_rev":"2-e25334d60ab7d2c1753d8871a5abc33d","doc_type":"DocCollections","url_name":"favorite-flickr-photos","name":"Favorite Flickr Photos","content1":"f5ba37e5af406ab079d596f7a175ab4a","user":"tabdon","content5":"f5ba37e5af406ab079d596f7a157a953"}} ]} – tabdon Apr 29 '11 at 21:27

2 Answers2

5

?callback=

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

{"total_rows":6, ...

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.

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

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

invalid label

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 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.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Thanks - you set me on the right track. Through playing around with local json files instead of pulling from the CouchDB server, I was able to figure out (theory, anyway) that the .getJSON doesn't know what to do with the first part of that JSON string. Once I got the CouchDB server to give me a JSON reply without the total_rows stuff I was able to parse it successfully. I'm not that sharp with JSON yet, so perhaps there's a way to get around the total_rows stuff if it's there. – tabdon Apr 30 '11 at 18:29
0
var key = json.rows[0].key;
var doctype = json.rows[0].value.doc_type;

so on and so forth...

Kevin Ennis
  • 14,226
  • 2
  • 43
  • 44