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.
Related
I was using the code bellow to retrieve via ajax a value from a database and then translate it with t object (dictionary).
Sometimes it is translated other's nothing appears.
In the beginning of the page i read a file dictionary to an objet (t). Could it be because of the time to read de file?
I am stuck :\ I would appreciate very much if someone could give an hint how to solve this mistery?
var json = JSON.parse( response ); // success function
var type_aux=t[json[0].type];
alert("->"+type_aux+"<-");
$('#TYPE_SHOW').html(type_aux);
I have a Node.js app which, among other things, responds to a AJAX (jQuery) $.get() request from a Web page by sending some HTML back to that page. The app uses Express.
So in the server code, I have:
app.get('/friends', api.friends);, where api is defined as api = require('./static/routes/api') and i'm setting app.use(app.router);.
In myapi.js module, I have api.friends code: I have
exports.friends = function(request, response)
{
...lots of code...
};
wherein I create some specific HTML.
Now, my question is: How do I actually send this HTML back to the client? I can't use the passed-in response object because this is no longer an Express-type response object, so the usual reponse.send(), .end(), etc. methods don't exist.
I have no idea what to do, reflecting a lack of understanding of Node and its innards (this is my first Node app), so any and all help will be greatly appreciated and welcomed. Thank you.
As #Daniel stated in his comment, the response object is certainly an Express object, and you can return your HTML simply by rendering a view, like so:
exports.friends = function(request, response) {
//do stuff
response.render('friends.html');
};
Of course, you would have to define your views in your app.js setup, with something like this:
app.set('views', __dirname + '/views')
Ugh! I am such an idiot! In my exports.friends handler, there's a request going out which, as a part of its calling parameters, takes a function of the form function(error, response, body). Note the response parameter. I was sending the results within this function, which of course used this response object and not the one passed through exports.friends(request, response). Doh. Oh well - thank you anyway - you made me look at the code again, and with the persepctive of knowing that the response object was legitimate, I was able to see the error. Thank you again - appreciated!
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.
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
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.