reading data from Ruby in D3 (JSON) - ruby

i'm confused how to read data from ruby class.
assume i have a class, called "Points".
what i want is reading the all data from this 'Points'.
what i did is like this :
var allPoints = <%= Point.all.to_json %>;
d3.json('allPoints',
function(data){ .......}
i dont know why but somehow d3 can't read the varable allPoints.
is there maybe something i forget to put in code?
or maybe there's another way to read all data from Ruby?

d3.json() is used for performing an AJAX call and retreiving JSON data from a server, which is then passed to the provided callback. It appears that what you're doing is generating a page with your data embedded in the page as a JavaScript. To use this object, just use it. Use whatever code was in your callback function, replacing the name data with allPoints.

Related

JSONP, do you have to change your JSON file?

Can someone help me understand JSONP a little better?
I have a json file being out putted to a url. But due to same-origin policy I need to use JSONP. My question is do I need to change the actual JSON file or will using an ajax call with jquery, dataType: 'jsonp' do the work for me?
JSONP is nothing but, JSON with padding i.e. JSON wrapped by a function call. This format helps to pass the JSON data to java script.
JSON came into picture, when the JSON i.e. java script object can be used to represent the data, which was previously represented in the form of XML.
For example,
var data={...}; is data in json format. Whereas In JSONP, same data is written as getData(data);
In your scenario of ajax call, dataType:'jsonp', json data has to be passed as an argument to a function. You can access the response in that function.
If you could have provided some code, it will be easy to resolve your query. Information about JSONP is available on wikipedia here.
You will have to wrap your JSON data in a function call.
Like, someFunctionName(YOUR_EXISTING_JSON_DATA);
And,
use someFunctionName as jsonp callback
See, Cross-domain communications with JSONP

ajax request - how to read the json obj

I am using ajax request and getting a json response. i do not how to read this
[{"systemReviewInfoMapListSize":1,
"diagnosis":"Impaired hearing\nEarache \nEar noise",
"isClinicalSummaryAvail":"false","isSymptom":"true",
"isDiagnosis":"true",
"symptom":"Impaired hearing\nEarache \nEar noise",
"isSystemReviewInfo":"true"}]
I assume you're setting a variable as the ajax response?
if so you could do this:
var objJSON = ajax.response;
var systemReviewInfoMapListSize = objJSON.systemReviewInfoMapListSize;
var diagnosis = objJSON.diagnosis;
etc...
Hope that makes sense... basically your ajax response returns an object full of name:value pairs. You access the values within the json like you would access the properties of any other js object, by taking the variable storing the object and appending .PropertyName.
Take a look Here
Hope someone will clarify me why the downvote, not that it matters, I've always though SO was a community to enrich our knowledge about programming and not another code fragment repository.

jquery ajax data string. How do i make an object or escape the string?

I have a button which calls jquery.ajax and submits POST data. I need to get user text from a textarea. So far, no problem. However now i need to set the post data. I have a string in the form as k=v&k2=v2 etc and then i have this user text. I obviously cant write + "&usertext=" + usertext since the text may have code and &kxxx=val which should be inside of the usertext value.
How do i set the ajax data?
http://api.jquery.com/serialize/
You can call the jQuery method serialize on a form to get a valid data structure to pass in to jQuery.ajax instead of building it from scratch:
$.ajax({
data: $('#myform').serialize()
});
For the values outside of your textarea you can then add them as hidden inputs in your form and they'll be pulled in to the resulting serialize data.
there are at least 2 options:
1. use http://jquery.malsup.com/form/ jQuery plugin, see 'ajaxSubmit' method
2. use 'serialize' method of jQuery (seee here for details)

Create Local Javascript Array From jQuery.get() AJAX Call

I'm using the jQuery Autocomplete plugin and would like to store all the autocomplete values locally in an array. I'm able to retrieve a comma separated list from a url using jQuery.load() or jQuery.get(). However, I can't load the data into an array successfully.
Any suggestions on how to get this working? I realize there's probably a much better way. Any help would be appreciated.
Thanks!
Are you sure that it isn't working properly, but that you're not trying to access that variable outside of the async ajax call?
The only way to have immediate access outside of that ajax call is to run it async=false, or to use a timer to wait and check for that value.
If this is the case, look to the docs on the ajax abstractions:
http://api.jquery.com/jQuery.ajax/
Specifically:
var html = $.ajax({
url: "some.php",
async: false
}).responseText;
When you say "comma separated list" I assume you mean a string (like "foo1, foo2, foo3")?
If so, you can use the split function of the string type like so:
var data = "foo1, foo2, foo3";
data.split(",");
// ["foo1", "foo2", "foo3"]

jQuery POST and GET methods: Construct URL or use data param?

I am using the post and get methods for Ajax calls, and have a general question. There are two methods I've seen people use on the web:
Construct the URL and parameters by
hand
Use the data parameter
Both approaches work. I've included them below:
// Construct the POST URL by hand
queryStringDelimiter = "?";
settings.queryParam = "q";
$.post(settings.url + queryStringDelimiter + settings.queryParam + "=" + query, {}, callback, settings.contentType);
// Use the data param
$.post(settings.url, {q:query}, callback, settings.contentType);
Are there any situations where you would construct the URL and parameters by hand instead of using the built-in data parameter? Any advantages of one method over the other?
I'd say the data approach is better since it formalizes the process and reduces the chances of producing errors while string building. Besides, the JQuery library will do the string building for you so its basically the same amount of work.
No reason I can think of why one would construct them by hand unless they didn't know of the data parameter if there's more than 1 or 2 parameters, it's also cleaner to keep them separated so if you have to loop through the data object and possibly modify some values you'd just iterate over the object instead of parsing a string manually.
If you let jQuery concatenating the data in to the appropriately formatted string you...
avoid having to duplicate that code...
avoid worrying about escaping the data for transport...
can easily switch between GET and POST requests in the future...
Really, the only argument AGAINST using the data parameter is if you already have the data in a concatenated format.
If I am using a GET I tend to just construct the URL, but when using POST I use the data parameter.
I do it because it is closer to how I was doing ajax calls before jQuery, when I wrote everything myself.

Resources