jQuery().ajax and same response as used by Query()

183 views
Skip to first unread message

NA

unread,
Jan 12, 2012, 5:27:44 PM1/12/12
to Google Visualization API
Hi,

I currently use the Query() method to request data from my server. My
server responds with text that looks something like:


google.visualization.Query.setResponse({"status":"ok","reqId":"0","table":
{"cols ...

All is good so far. :)

I want to make requests to this same server, but I want to do them
using jQuery. You have a nice example of how to do this here:

http://code.google.com/apis/chart/interactive/docs/php_example.html

It looks like the server produces the same data as in the "table"
field of the Query() response.

My question is, how can I process the output of server's
"google.visualization.Query.setResponse..." message so that I can use
it with the jQuery approach that only needs the table field? I don't
want to change the server's output. I'm hoping there's a way I can
process the response and extract the data using regular js and jQuery.

Can I just evaluate the string and extract the table somehow?

(if you're curious, I'm doing this as a workaround to not having a
POST method available in the Query() object; POST is available in
jQuery )

I feel like it's possible, but I'm just missing some info on how to do
it properly.

Thanks for any suggestions,

asgallant

unread,
Jan 13, 2012, 9:30:12 AM1/13/12
to google-visua...@googlegroups.com
I believe that if you cut the object out of the string and eval it, you should be able to access the table.  Something like this, perhaps?

// response contains query response
// cut off everything up to the first '('
response = response.replace(/^.*?(/, '(');
// cut off everything after the last ')'
response = response.replace(/).*?$/, ')');
// eval what's left
var responseObj = eval(response);
var data = new google.visualization.DataTable(responseObj['table']);

NA

unread,
Jan 17, 2012, 2:12:22 PM1/17/12
to Google Visualization API
Hi,

That's what I ended up doing. With some minor tweaks to the regexp
expressions, it works well. (If anyone wants to see, let me know and
I'll post it later tonight. I'm not at the computer that has access
to that code right now).

thanks again for the helpful suggestion,

NP

unread,
Jan 17, 2012, 11:22:23 PM1/17/12
to Google Visualization API
You might not need to do a regex. If
google.visualization.Query.setResponse returns JSON, then you can just
eval it directly.
Something like
response = google.visualization.Query.setResponse(...)
response = eval(response);
var data = new google.visualization.DataTable(response.table);

NA

unread,
Jan 18, 2012, 11:08:09 AM1/18/12
to Google Visualization API
Hmmmmm... I don't think that will work.

The response does return JSON. It returns JSON that looks like:


google.visualization.Query.setResponse({"status":"ok","reqId":"0","table":
{"cols ...

Are you saying I can pass this string to the method
google.visualization.Query.setResponse? That seems a little odd,
since the response itself contains a call to
google.visualization.Query.setResponse().

I also didn't think this would work because of the reqID. If you eval
the string directly, without wrapping it in another
google.visualization.Query.setResponse(), you get an error because
google.visualization.Query doesn't recognize the reqId (which makes
sense, since google.visualization.Query never sent the request in the
first place). If I wrap it in a
google.visualization.Query.setResponse() call as you're suggesting, I
assume I'd get the same error.

I don't have a chance to test this now, though, so am just being
hypothetical, but I suspect it won't work. Sorry to sound so
pessimistic :)

asgallant

unread,
Jan 19, 2012, 9:48:55 AM1/19/12
to google-visua...@googlegroups.com
There's no particular need to execute the #setResponse method anyway, since all you need is the DataTable object.
Reply all
Reply to author
Forward
0 new messages