How can I get a SpreadSheet Google using XHR? - javascript

I'm trying to get Spreadsheet data Google. So, to do this, I've seen the Query Language Reference and basic information about Query object and, in this second link, it appears the "fast" info to realize a query. The problem is that, because of my JS skills, I don't understand opt_options parameter of the google.visualization.Query function.
I mean, I know that I have to send my query to get the data, but I don't know how to do it because the sendMethod description of opt_parameters doesn't seems me sufficiently detailed for write the JS code whether I want use the XHR method.
Also I've seen that I can include the query in the SpreadSheet link. Anyways, I only want to get the data transformed into JSON data or something that I can manipulate.
I consider that this question can be useful for this people like me that are beginning to get data from other pages and don't know how to do it exactly.
PD: Yes, I've read XMLHttpRequest info from MDN.
EDIT: I've supposed that Query object send XHR and I only have to concern me for send method of Query and its callback that is an instance of QueryResponse class. The problem is that I don't retrieve anything! Here is my code once again:
var query;
function getData(DATA_SOURCE_URL){
query = new google.visualization.Query(DATA_SOURCE_URL);
query.setTimeout(10);
query.send(displayData);
console.log("Query has been sent");
}
function displayData(response){ //This function doesn't works
if(response.isError()){
console.log(response.getDetailedMessage());
}
var data = response.getDataTable();
alert(data);
}
Thanks in advance!

(Posted answer on behalf of the OP).
Finally I got rid of Query Language Reference and all stuff about Google and I discovered TableTop to request data from spreadsheets. It's very easy to use and its documentation is simple and concise. My main aim was bring data from a spreadsheet to my JS application.

Related

how to handle csv data from an api get call to a post call

I couldn't find any useful answer in similar questions.
Basically I have an api that responds with a list of data in csv form.
Can see all this data in postman.
I want to use that data as a param on another api in a 'post' method.
The questions are:
how do I handle csv data in javascript so that I can store it in a variable and use it in the aforementioned post api call as param, the data being a list of values.
I realise I am not posting any code, I am happy to be pointed in the right direction as can find anything clear in googleland. So far when I just call the api and store the response in a variable, it gives me nothing when I try to console log it.
any guidance is highly appreciated.

What is the best way to manipulate an API AJAX JSON response and pass it to another page?

I think I have a tough one for you guys. Or at least it's been tough for me. I've been searching for the best way to do this in Stack Overflow and everyone that has asked has been given a different response.
I have this code that is accessing an API and calling a maintenance list of all the vehicles in a fleet.
function getMaintenanceList() {
var settings = {
"url": "API URL HERE",
"method": "GET",
"timeout": 0,
"headers": {
"Authorization": "Bearer token here"
},
};
$.ajax(settings).done(function (response) {
// The response the API sends is a JSON object.
// It is an array.
var jsonMaintenance = response;
var parsedJson = JSON.stringify(jsonMaintenance);
//Left over code from when I was trying to
//pass the data directly into the other page
// I was unable to do so
//return jsonMaintenance;
//Left over code from when this was in a PHP file
//and I was posting the stringified response to the page
// for testing purpose
//I had to disable CORS in Google Chrome to test the response out
//console.log(jsonMaintenance);
//document.getElementById("main").innerHTML = parsedJson;
});
};
The code above works well. What I was attempting to do here was write the stringified response to a file. Save that file in the server. Call it from another page using JavaScript and save it as an object in JavaScript, parse it using JSON.parse(), and then pull the required information.
Here's an explanation as to why I'm trying to do it this way. When I call the maintenance list from the API, I'm getting the entire maintenance list from the API, but I need to be able to display only parts of the information from the list.
On one page, we'll call it vehicle-list.php, on it I have a list of all the vehicles in our fleet. They all have unit numbers assigned to them. When I click on a unit number on this page it'll take me to another page which has more information on the vehicle such as the VIN number, license plate, etc. we'll call this page vehicle-info.php. We're using this page for all the vehicles' information, in other words, when we click on different unit numbers on vehicle-list.php it'll always take us to vehicle-info.php. We're only updating the DOM when we go to the page.
I only want to include the information specific to each vehicle unit in the page along with the other info in the DOM. And I only want to call the info from the API once as I am limited to a certain amount of calls for that API. This is why I am attempting to do it this way.
I will say that what I originally wanted to do was get this JSON response once every 24 hours by using a function in vehicle-list.php save the reponse as a variable as seen above var jsonMaintenance = response; and then just access certain parts of the array every time a unit number is clicked. However, I have been unable to access the variable in any other page. I've written many test files attempting to call jsonMaintenance without success so I've been trying to to just save it as a text file to the server and I haven't been able to figure that out either.
After explaining all of the above. My questions are these:
How do I best manipulate this data to accomplish what I want to accomplish? What would be the best standard? Is the above code even the right way to call the data for what I'm trying to do?
There doesn't seem to be a set standard on accomplishing any of this when I search on Stack Overflow. I'd like to be as efficient as possible.
Thank you for your time.
there is a lot of ways how you pass your data through your website after getting it in from an api call, the best approach is to store these information in a database and call it back in which ever way you want, you can do that as far as you are using php, you can store it to sql or to access, if you don't want to store these information in a database like in sql or access, then best way is to store it to localStorage and call it back whenever you want.
I will show you briefly how you can do that, if you want better explanation post an example of your returned data.
to store an item in localstorage use,
localStorage.setItem('key', 'value');
to call an item back from localstorage use,
var somevar = localStorage.getItem('key')
to remove specific item from localstorage use,
localStorage.removeItem('key')
to clear all items saved to localstorage use,
localStorage.clear()
be aware storing the data to localStorage is only at the station you are using
I would do it somehow like this.
Call the maintenance list from the API with the server side language of your choice which seems to be PHP in your case. Lets say the script is called: get-list.php. This can be triggered by a cron job running get-list.php in intervals limited to the certain amount of calls that you are allowed to do for that API. Or if you are not able to create cron jobs then trigger the same get-list.php with an AJAX-call (eg jQuery.get('sld.tld/get-list.php') - in this case get-list.php have to figure out if its the right time to call the API or not).
Now that you have the data you can prepare it as you want and store it as a JSON-string in a text file or database of your choice. If I get you right you have a specific dataset for each vehicle, which have to be identified by an id (you named it "unit number") so your JSON would look kind of: {"unit1": { property1: "val1", property2: "val2" }, "unit2": { property1: "valXYZ", property2: "valABC" }} or alike.
Now when you link to vehicle-info.php from vehicle-list.php, you do it like so: ancor or similar as well. Of course you can also grab the data with AJAX, its just important to deliver vehicle-info.php the corresponding unit number (or id - better to say) and you are good to go.
vehicle-info.php now have all there is to render the page, which is the complete data set stored in text file or data base and the id (unit number) to know which part of the whole dataset to extract.
I wanted to give you this different approach because in my experience this should work out just so much better. If you are working server side (eg PHP) you have write permissions which is not the case for JavaScript-client side. And also performance is not so much of an issue. For instance its not an issue if you have heavy manipulating on the data set at the get-list.php-level. It can run for minutes and once its done it stores the ready-to-use-data making it staticly available without any further impact on performance.
Hope it helps!
If i ran into a similiar problem i would just store the data in a database of my own and call it from there, considering you are only (willing/abe/allowed) to request the data from the API very rarely but need to operate on the data quite frequently (whenever someone clicks on a specific vehice on your applicaiton) this seems like the best course of action.
So rather than querying the data on client side, I'd call it from server, store it on server and and have the client operate on that data.

I need to make a PUT request to a JSON file in an external API. I need to update specific fields

I have a JSON on an external API, and, in the documentation of the API, it instructs to use a PUT request to update records. I have read a lot on this, but I haven't found an adequate amount of information.
I will do my best to provide you with the most info I can so you can help me, specifically.
I have the url stored in a variable: $record.
The documentation instructs to: Set the request body content as a json formatted array of record data. This is the “payload”.
It also provides an example:
{
“Name”: “Craig J. Peters”,
“Job Title”: “Director of Engineering”
}
I need to change specific fields. How can I accomplish this using cURL and PHP?
I should mention: If this is easier with JavaScript, I am open to that as well.
I strongly recommend you to check Guzzle as a starting point -
http://docs.guzzlephp.org/en/latest/request-options.html#body

Getting data using RESTful and backbone.js

I've been reading tons of tutorials, I really like this technology, but I am missing something.
Following this tutorial http://www.youtube.com/watch?v=FZSjvWtUxYk you get JSON data from this url, http://backbonejs-beginner.herokuapp.com/users then you set your model, and collection.
I am not sure how to set this url on my own (for either local storage or database storage), I obviously need to set it to where I can GET the data or PUT/POST/DELETE data. I need to mention that I dont want to use PHP to send the data to the database, because this is a cordova app. Maybe setting data is beyond just using a URL, hopefully some one can make this clear, this should be easy points for most.
I am missing something easy here, because there is a reason they are not covering this topic extensively (maybe they are and I am overlooking it), but oh well ill ask anyways maybe someone can point this out :)
Here is the ajaxPrefilter it kind of confuses me, I know it obviously prefilters our url to point to the data, but I am not sure how to apply an appropiate url for local storage/server side.
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = 'http://backbonejs-beginner.herokuapp.com' + options.url;
});
Edit: Also I noticed that when I run a POST request, this url changes http://backbonejs-beginner.herokuapp.com/users and adds my data, this is from the RESTful services right, and what is so special about that url that it allows me to do that? In the meantime I am going to keep rewatching the backbone tutorial videos, to see what I am missing lol.
Also watching this http://net.tutsplus.com/tutorials/javascript-ajax/understanding-backbone-js-and-the-server/ starting from the beginning he actually understands what I mean, but jeffery uses PHP, which maybe I can use with cordova (pointing to a script or something) but I would rather use javascript to send data to mysql and local storage.
EDIT: Okay I see heroku is a cloud based service https://www.heroku.com/ so inside that url we were using a dummy type of service to store the data.. Okay so now how do I point the url to local storage?
Finally got it!
I came across this Backbone.js + Java Wine Cellar Application. It is an application with a Backbone front end and a Java backend. You have just to download it, set up the environment and see how it works.
There is also a tutorial, the author wrote it also in PHP, just check his blog.
Okay, so I am a noob, I am sure the way I asked this question may have gotten confusing but here is what I did.
I spent a good amount of time figuring out how to use https://github.com/jeromegn/Backbone.localStorage what I learned is, models and collections are well models and collections of data. I didn't think of it like that(should have been transparent from the get go, but oh well) inside the model and collection you need some data and so from the tutorial I followed the method of getting data was via http://backbonejs-beginner.herokuapp.com/users it contained jSON data which we could work with.
So in using local storage, we need to change where our jSON data is by using localStorage: new Backbone.LocalStorage("choose-some-identifier"),
So understanding the model and collection really helps in knowing what to do. Now I just need to figure out how to add localstorage data to a server, and since I am using phonegap it would be cool to add an event listener for when the user connects to the internet and on that event submit the local storage data to the server.

Weatherbug REST JSON

I have a solid understanding of HTML and javascript. I have no experience with JSON. I would like to get the data from the URL http://i.wxbug.net/REST/Direct/GetUv.ashx?zip=21044&api_key=vxwdyz3evgtvuv9d5e53sckc and display it on a webpage. I have looked around for a simple tutorial that explains how to retrieve JSON data from a URL without prevail. Can someone point me in the right direction?
If your page is in different domain, you will encounter the "...not allowed by Access-Control-Allow-Origin" error when you use javascript to access it. However, you could make a proxy page in the server side, fetch the data and then output data.
And then, you could get the data, for example, if you use jQuery, it is very easy like:
$.getJSON(your_proxy_page_url,function(data) {
console.log(data);
});
You could also try to take look at this post, maybe it can help you out:
http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getScript

Categories

Resources