Is There anyone who knows what this piece of code does? - javascript

I am very new to javascript .. and my teacher provided us with this piece of code along with html and css file which on any date you enter gives you images of mars taken from ISS..
Would anyone care to explain what is happening inside the data: {} and how to get such API URL'S? I mean from where do we get that?
var input_box=$('#div-date input');
var button=$('#div-button button');
var container2=$('#container2');
button.click(function()
{
$.ajax({
method: "GET",
url: "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos",
data:
{
earth_date:parseInt(input_box.val().split('/')[2]).toString()+"-
"+parseInt(input_box.val().split('/')[1]).toString()+"-
"+parseInt(input_box.val().split('/')[0]).toString(),
api_key:"zrVafsMHD8r1SC8mHyg91mnNguuzdIoPRXGD1BvS"
},

The ajax method is a call to NASA endpoint where you are doing a GET call to the URL. I thinks this is clear.
Now, what does data?
earth_date is a value with the actual date parsed to a specific format. It get the date from the input_box with format 00/00/0000 and parse to format 0000-00-00
api_key is the key you need to authenticate and get the data.
And data itself is the way to send variables into URL. So, your url looks like this:
https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date=YOUR_DATE&api_key=YOUR_API_KEY
Note that is compound by:
url+ ?earth_date + api_key.
Updated to answer the question:
The API key is a way to secure the API.
An API key is a way to identify who is calling the endpoint. So only calls that have the API key will be able to reach the endpoint (or whatever the developer want).
Is an string generated by the owner so the only way to get the API key is NASA to give it to you. I suppose NASA give free API keys, but is not a "general string" or something like that. Every organization will secure their apis in a way, NASA provide api keys to reach his 'free' endpoints.

The code in the data transform the date format '30/12/2019' to '2019-12-30' since only the later format is valid for the request.

Related

get only specific element of a JSON database stored in server by url request

my website relies on a database which is a big JSON file like this:
var myjsonData =
[ {
"ID": 0,
"name": "Henry",
"surname": "McLarry",
"...": "...",
}]
I do generate this data every month at high cost to me, therefore I would like to avoid calling it straight in my html <head>, because this will allow any user to download the full database in no time.
I would like to build a "something" that can only call specific items from the json file (just the only one I want to show) without "exposing" the full .json onto client side.
today I use the call
var myvar= myjsonData.ID.Name
to get "Henry" into myvar, I would like to build something like
var myvar = mycallfunction(ID,Name)
I did try with PHP as intermediary but the ajax calls from javacript doesn't allow me to fetch the data.
Can I use JQuery with the JSON Url to get only the item I need?
What you can do is parse your json for an object. So you can get any value you want from json.
Example:
var myjsonData = '{"ID": 0,"name": "Henry","surname": "McLarry"}';
obj = JSON.parse(myjsonData);
console.log(myjsonData.ID); //print the id
console.log(myjsonData.name); //print the name
console.log(myjsonData.surname); //print the surname
So you have a NoSQL Database which has only one kind of Document that is the full JSON element you use in your website. In that scenario you have three options:
Depending on the NoSQL Database you're using you can limit the fields which will be returned(I.e: For MongoDB you can look here: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/)
Change the way you store you data into more modular documents and make the logic to connect them in you application. So instead of one big document you'll have modular ones as Users, Products, Transactions and etc and you can use your application to query them individually.
Build a Server Side logic as an API to deal with your data and provide only what you need, so the API(Which can be node.js, php, or any you may like) will get the full JSON it`s endpoints will only the data you want. For example: myapi.com/getUser, myapi.com/getProducts and so on.
If you're able to provide more info on the technologies you're using that would help us. Hope that helped :).

How can I make my API website json value into a new variable?

I need to convert a username to an ID. Here’s my API link: https://api.mojang.com/users/profiles/minecraft/username (username is replaced by the variable “name”.) So from that link I need to get the ID value and save it to a variable called “uuid”.
Thanks,
Nathan
Here’s a valid url for viewing: https://api.mojang.com/users/profiles/minecraft/_Pine
So, when you fetch the JSON object from that API, you need to parse it using JSON.parse(). So, for example:
let myUserInfo = JSON.parse(fetchResult);
let uuid = myUserInfo.id;
So, given that you currently don't know about fetch(), I'd suggest learning that. fetch is a javascript Promise object, a new part of the language that greatly simplifies grabbing data from open API's (for example). Here's a simple fetch statement for that URL:
let myUserUrl = "https://api.mojang.com/users/profiles/minecraft/_Pine";
fetch(myUserUrl)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson.id);
});
Note that this won't run on stackoverflow, as it crashes into a cross-origin error. Depending on where and how you are serving this, let us know if it works for you, or if you get stuck.

How to read data response from Wikidata API?

I am making a ReactJS application to show information about people when they search for it. I am trying to use MediaWiki to get the information about a person from Wikipedia. I am making an API call and the response is very weird. I am getting this whole bunch of "claims" in the API response from which I don't know how to take the useful info
This is the response I get:
Now what should I do with these claims? The information itself is scattered there. For example: "Date of birth" key is in some other claim and the value of "Date of death" is in some other claim. I want to show information like this in my page:
How can I do it? I couldn't get any guide for this.
All information you need for item Q2038656 is in your response. For example:
P18 is image - located in P18/mainsnak/datavalue/value/Vinod Khanna at Esha Deol's wedding at ISCKON temple 11 (cropped).jpg (check here how to get the link to the image)
P19 is place of birth - in P19/mainsnak/datavalue/value/id/Q1113311 (you can get the name of this item by this query: Peshawar)
P569 is date of birth - in P569/mainsnak/datavalue/value/time/+1946-10-06T00:00:00Z
...continue in the same way for all other properties.

getJSON error - not sure where I'm going wrong

With the help of a few other stack users I have come up with this:
http://jsfiddle.net/9ywLq/
I want to use an external json file & come up with something similar to this:
http://jsfiddle.net/RCB9M/
The file I am linking to at the moment is: http://www.lofiz.co.uk/afba/songkickwidget/testjsondata.json but this will ultimately change to a Songkick JSON output.
You are a victim of the same-origin policy. The browser will by default block JSON that is fetched cross-domain for security reasons. If you need to get data cross-domain, you will either have to implement a server-side proxy, or use JSONP, if the data is provided in that format as well. If the source doesn't provide data in JSONP, I believe you could utilize YQL to convert it.
Resource on JSONP: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/
Update: Your URL would be called like this:
​$(function(){
var url = "https://api.songkick.com/api/3.0/artists/253846/calendar.json?apikey=HlgKnFaq9qYO1h9T&jsoncallback=?";
$.getJSON(url, function(data){
// Do what you want to do with the return data within this callback
console.log(data);
});
});​
Update 2:
I have updated your JSFiddle to work with getJSON(). I also took the liberty to make a few other changes:
I modified how you defined your array of month-names, to make it more readable.
.getDay() in JavaScript is used to return the day of the week (0 = sunday, 1 = monday and so on), as I believe you want the day of the month, you should use .getDate() instead. See my JSFiddle.

Alexa Pagerank using jquery getJSON

I am trying to get the alexa pagerank via http://data.alexa.com/data?cli=10&data=snbamz&url= + weburl
Here is the url for getting the alexa rank of facebook.com
http://data.alexa.com/data?cli=10&data=snbamz&url=http://www.facebook.com
If you copy the link above and paste in your browser you can see that it is in xml format.
I am jusing getJSON to retrieve the alexa rank from other domain. I am also using yql api. you can see the url in the first parameter below.
var url = "http://data.alexa.com/data?cli=10&data=snbamz&url="+ inputtedURL;
OR to be specific
var url = "http://data.alexa.com/data?cli=10&data=snbamz&url=www.facebook.com";
Below is my getjsON
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+
"%22&format=xml'&callback=?",
function(data){
}
);
How will I get the contents of this http://data.alexa.com/data?cli=10&data=snbamz&url=http://www.facebook.com since this is in xml format. the getJSON returns JSON objects.
Any other suggestions?? I am building an alexa page rank checker using jquery only .
I see I'm a bit late to the party, but I had to solve this today and thought somebody might find it useful: You can use Yahoo Pipes to convert XML data to JSON.
I created an example Pipe that take a 'site' parameter, passes it to Alexa's API, and returns the result. You can simply add '_render=json' to the query and get the results back in JSON. So using jquery...
var pipe = "http://pipes.yahoo.com/pipes/pipe.run?";
pipe += "_id=c43fc0ed13fc8945cf438da90bff0121";
pipe += "&site=" + encodeURIComponent(url);
pipe += "&_render=json&callback=?";
$.getJSON(pipe,
function(data){
}
);
(Feel free to clone the Pipe and do with it as you will.)
Here's an example call to get Alexa stats for StackOverflow in JSON:
http://pipes.yahoo.com/pipes/pipe.run&_id=c43fc0ed13fc8945cf438da90bff0121&_render=json&site=http://stackoverflow.com
The prettified output looks like this:
{"count":1,"value":{
"title":"AlexaData",
"description":"Pipes Output",
"link":"http:\/\/pipes.yahoo.com\/pipes\/pipe.info?_id=c43fc0ed13fc8945cf438da90bff0121",
"pubDate":"Wed, 09 Jan 2013 21:50:25 +0000",
"generator":"http:\/\/pipes.yahoo.com\/pipes\/",
"callback":"",
"items":[{
"AID":"=","HOME":"0","URL":"stackoverflow.com\/","VER":"0.9",
"DMOZ":{
"SITE":{
"BASE":"stackoverflow.com\/",
"DESC":"A language-independent collaboratively edited question and answer site for programmers. Questions and answers displayed by user votes and tags.",
"TITLE":"Stack Overflow",
"CATS":{
"CAT":[
{"CID":"377304","ID":"Top\/Computers\/Programming\/Resources\/Chats_and_Forums","TITLE":"Resources\/Chats and Forums"},
{"CID":"477752","ID":"Top\/Reference\/Ask_an_Expert\/Computers_and_Technology","TITLE":"Ask an Expert\/Computers and Technology"}
]
}
}
},
"SD":{
"POPULARITY":{"SOURCE":"panel","TEXT":"86","URL":"stackoverflow.com\/"},
"REACH":{"RANK":"81"},"RANK":{"DELTA":"-3"},
"COUNTRY":{"CODE":"IN","NAME":"India","RANK":"25"}},
"description":null,
"title":null
}
]}
}

Categories