You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Matthew Woodward <ma...@mattwoodward.com> on 2010/11/09 18:49:20 UTC

Line Breaks in JSON Causing Issue With jQuery

Hello fellow relaxers--I'm fairly new to CouchDB and loving it so far, but I
ran into a bit of an issue when calling a view directly from jQuery. I'm
sure I'm just missing something obvious, but here goes.

I have a view in CouchDB that I'm calling from jQuery like so:

var couchURL = "
http://server:5984/database/_design/mydesigndoc/_view/viewName";
$.ajax({
    dataType: "jsonp",
    url: couchURL,
    jsonpCallback: "myCallback",
    success: myCallback
});

The view works fine when I hit it in a browser. The problem I'm having is
that, I believe because of the line break after the first array bracket in
the "rows" element, jQuery doesn't see this as valid JSON. The error I get
is "invalid label" and it appears to break right at the line break (at least
according to Firebug).

Any suggestions welcome. Thanks!

-- 
Matthew Woodward
matt@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Re: Line Breaks in JSON Causing Issue With jQuery

Posted by Matthew Woodward <ma...@mattwoodward.com>.
For anyone else coming across this thread, I enabled JSONP in my ini file
and restarted CouchDB, did a bunch of experimentation, and finally hit on
what works (for me anyway):

var dbURL = "
http://server:5984/database/_design/designname/_view/viewname?callback=?";
$.getJSON(dbURL, null, function(data) { alert(data.rows[0].value.whatever);
});

If the alert looks right (and use Firebug to check your full response of
course), then you should be in good shape.

Firebug still complains in some cases (I tried a ton of iterations of things
so I'd have to retrace my steps to see where exactly this occurs) because of
the line breaks, but that seems to be a red herring.

This is CouchDB 1.0.1 on Windows Server if that makes a difference to
anyone.

Thanks for the help!

--
Matthew Woodward
matt@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Re: Line Breaks in JSON Causing Issue With jQuery

Posted by Matthew Woodward <ma...@mattwoodward.com>.
On Tue, Nov 9, 2010 at 9:52 AM, Mikeal Rogers <mi...@gmail.com>wrote:

> jsonp isn't enabled by default so it may actually not be embedding the
> callback in the response. Might want to try just normal json instead. To my
> knowledge we don't add any line breaks between json elements.
>
>
Thanks--I'll try regular JSON in a bit. Was getting an error on that as well
but I'll look back into it.

In the mean time, when I call my view I'm definitely getting a line break in
both a browser and in curl, e.g.:
{"total_rows":4641, "offset":0, "rows":[
{"id":"myid", "key":"mykey", "value":"myvalue"},
{"id":"myid2", "key":"mykey2", "value":"myvalue2"}
]}

Note the line break after the [, between each record, and before the final
]}

Is there something I did in my view that would be causing that to happen?
Are the results I'm seeing in a browser and in curl not necessarily
"accurate" as far as the formatting goes?

Thanks.
-- 
Matthew Woodward
matt@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

Re: Line Breaks in JSON Causing Issue With jQuery

Posted by Mikeal Rogers <mi...@gmail.com>.
jsonp isn't enabled by default so it may actually not be embedding the
callback in the response. Might want to try just normal json instead. To my
knowledge we don't add any line breaks between json elements.

-Mikeal

On Tue, Nov 9, 2010 at 9:49 AM, Matthew Woodward <ma...@mattwoodward.com>wrote:

> Hello fellow relaxers--I'm fairly new to CouchDB and loving it so far, but
> I
> ran into a bit of an issue when calling a view directly from jQuery. I'm
> sure I'm just missing something obvious, but here goes.
>
> I have a view in CouchDB that I'm calling from jQuery like so:
>
> var couchURL = "
> http://server:5984/database/_design/mydesigndoc/_view/viewName";
> $.ajax({
>    dataType: "jsonp",
>    url: couchURL,
>    jsonpCallback: "myCallback",
>    success: myCallback
> });
>
> The view works fine when I hit it in a browser. The problem I'm having is
> that, I believe because of the line break after the first array bracket in
> the "rows" element, jQuery doesn't see this as valid JSON. The error I get
> is "invalid label" and it appears to break right at the line break (at
> least
> according to Firebug).
>
> Any suggestions welcome. Thanks!
>
> --
> Matthew Woodward
> matt@mattwoodward.com
> http://blog.mattwoodward.com
> identi.ca / Twitter: @mpwoodward
>
> Please do not send me proprietary file formats such as Word, PowerPoint,
> etc. as attachments.
> http://www.gnu.org/philosophy/no-word-attachments.html
>