Having trouble using data returned by Ajax.request

1 view
Skip to first unread message

benca...@gmail.com

unread,
Jul 10, 2007, 7:09:29 AM7/10/07
to Ruby on Rails: Spinoffs
Hello everyone, I'm new here.

I've been working with prototype and plotr for about a month now, off
and on, and I have pretty much hit the wall on using the data returned
by Ajax.Request.

I'm using some php code to return a string:

{'foo': [[0,0.0865334429075127], [1,0.0828179861705063],
[2,0.0828173042602942], [3,0.0841707718624196]]}

But I keep getting an error: "item.pluck is not a function" on line
857 of plotr_uncompressed.js

Here is the code I'm using to grab the data into a javascript
variable:

var graphData = "";
var url = "foobar.php";

function generateGraphData(dataRange,dataUrl) {
new Ajax.Request(url, {
method: 'get',
parameters: {dR:dataRange,dU:dataUrl},
onLoading: function() {
// change this to a loading image/html
$(dataRange).innerHTML="loading...";
},
onComplete: function(request) {
if(request.status != 200) {
// change this to a pleasant error message
$(dataRange).innerHTML="unavailable...";
} else {
graphData = request.responseText;
$(dataRange).innerHTML = graphData;
}
},
onFailure: function() {
notice.update("hrm, something went wrong...");
}
});
}

And here is the code I'm using to try to load the data into the graph:

function updateChart(chartName) {

var newOptions = {
padding: {left: 30, right: 0, top: 10, bottom: 30},
backgroundColor: '#f2f2f2',
shouldFill: false,
colorScheme: 'blue',
yNumberOfTicks: 2,
xTicks: [
{v:5, label:'jan'},
]
};

document.getElementById('weekdiv').innerHTML = '<div><canvas
id="newchart"
height="250" width="700"></canvas></div>';

var weekLine = new Plotr.LineChart('newchart',newOptions);
//weekLine.addDataset(dataset2);
weekLine.addDataset(graphData);
weekLine.render();

}


As you can see in the last bit of code, I've commented out the line:
"weekLine.addDataset(dataset2)". dataset2 is an array defined in
javascript on this page, that is identical to the string that is being
passed to the page from the php script.

Any help would be greatly appreciated!!


Ben

Diodeus

unread,
Jul 10, 2007, 2:29:15 PM7/10/07
to Ruby on Rails: Spinoffs
At the beginning of updateChart, add:

alert(graphData)

See what you're actually getting back. There might be some formatting
characters that you won't see when you put the same thing into a DIV.

On Jul 10, 7:09 am, "bencarl...@gmail.com" <bencarl...@gmail.com>
wrote:

Ben Carlson

unread,
Jul 10, 2007, 2:44:04 PM7/10/07
to Ruby on Rails: Spinoffs
That's a good thought, what I'm doing is similar, in the
generateGraphData() function, I'm doing this: $(dataRange).innerHTML =
graphData; which dumps the output from the PHP file to a <div/>.
Everything looks good there, but I keep getting the error mentioned in
the original post.

Ben

Diodeus

unread,
Jul 10, 2007, 3:08:29 PM7/10/07
to Ruby on Rails: Spinoffs
I think the problem lies here:

graphData = request.responseText;

it it getting set to the *string value* of the response.

Try graphData = eval(request.responseText)

Ben Carlson

unread,
Jul 10, 2007, 7:34:04 PM7/10/07
to Ruby on Rails: Spinoffs
Thanks Diodeus,

I'm not comfortable doing an eval due to XSS, so I'm going to pass an
XML file, then parse it in javascript. I think that's the best/safest
way to handle it.

Thanks,

Ben

jdalton

unread,
Jul 10, 2007, 11:01:53 PM7/10/07
to Ruby on Rails: Spinoffs
Hi Ben,

Prototype supports the handling of JSON in AJAX and in its extensions
of native JavaScript objects.
For a small tutorial on Prototype and JSON you can go here (they also
touch on XSS attack prevention that Prototype has).

http://www.prototypejs.org/learn/json

Don't forget to set your responce header (from the server-side to X-
JSON)

other info:
http://www.prototypejs.org/api/ajax/request
http://www.prototypejs.org/api/string/toJSON
http://www.prototypejs.org/api/string/unfilterjson
and so on ....

Diodeus

unread,
Jul 11, 2007, 2:27:09 PM7/11/07
to Ruby on Rails: Spinoffs
Ben, jdalton is right.

XML is awkward and slow.

I used to do things in XML then gave JSON a try. I'm never going back.

On Jul 10, 11:01 pm, jdalton <jdalton...@gmail.com> wrote:
> Hi Ben,
>
> Prototype supports the handling of JSON in AJAX and in its extensions
> of native JavaScript objects.
> For a small tutorial on Prototype and JSON you can go here (they also
> touch on XSS attack prevention that Prototype has).
>
> http://www.prototypejs.org/learn/json
>
> Don't forget to set your responce header (from the server-side to X-
> JSON)
>

> other info:http://www.prototypejs.org/api/ajax/requesthttp://www.prototypejs.org/api/string/toJSONhttp://www.prototypejs.org/api/string/unfilterjson
> and so on ....

Reply all
Reply to author
Forward
0 new messages