Last active
September 17, 2018 19:09
-
-
Save ant-rod-silva/e54a280b92994eb8bf125b06c5e2af43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* npm install xmlhttprequest | |
node testapinode.js | |
data output: | |
{ objects: | |
[ { id: 1, name: 'Afeganistão', code_iso3: 'AFG', code_iso2: 'AF' }, | |
{ id: 3, name: 'Albânia', code_iso3: 'ALB', code_iso2: 'AL' }, | |
... 147 more items ] } | |
*/ | |
// ******************************* | |
// get multiple data | |
// ******************************* | |
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; | |
var request = new XMLHttpRequest(); | |
var url = "http://localhost:8000/pt-br/api/country/?api_key=MyKeySecr3tAp1"; | |
request.open('GET', url, true); | |
request.onload = function () { | |
var data = JSON.parse(this.responseText); | |
if (request.readyState == 4 && request.status == "200") { | |
//console.log(data); | |
for(i = 0; i < data.objects.length; i++) { | |
console.log(data.objects[i].id + ' - ' + data.objects[i].name); | |
console.log('******************************'); | |
} | |
} | |
else{ | |
console.error("error retrieving data"); | |
} | |
} | |
request.send(null); | |
// ******************************* | |
// get unique data | |
// ******************************* | |
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; | |
var request = new XMLHttpRequest(); | |
var url = "http://localhost:8000/pt-br/api/country/1/?api_key=MyKeySecr3tAp1"; | |
request.open('GET', url, true); | |
request.onload = function () { | |
// Begin accessing JSON data here | |
var data = JSON.parse(this.responseText); | |
if (request.readyState == 4 && request.status == "200") { | |
console.log(data); | |
console.log(data.id + ' - ' + data.name); | |
} | |
else{ | |
console.error("erro"); | |
} | |
} | |
request.send(null); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment