bencarlson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-10 11:09 UTC
Having trouble using data returned by Ajax.request
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <bencarl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jul 10, 1:29 pm, Diodeus <diod...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <bencarl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jul 10, 2:08 pm, Diodeus <diod...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 .... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ....--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Possibly Parallel Threads
- GGplot 2 – cannot get histogram and box plot axis to match.
- Best Fit line trouble with rsruby
- Obtain gradient at multiple values for exponetial decay model
- Graphing: plot 3rd variable based on color gradient
- Obtain gradient at multiple values for exponential decay model