Last active
August 29, 2015 14:21
-
-
Save jmcanterafonseca/944855e22e7a1466b867 to your computer and use it in GitHub Desktop.
FIWARE - Query Context Data from Web Browser
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
// Don't forget to start Orion with the -corsOrigin option | |
// See also | |
var ORION_URL = 'http://130.206.83.68:1026/v1'; | |
var QUERY_CONTEXT = ORION_URL + '/' + 'contextEntities' | |
function queryContext(entityId) { | |
return new Promise(function(resolve, reject) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', QUERY_CONTEXT + '/' + entityId, true); | |
xhr.responseType = 'json'; | |
xhr.setRequestHeader('Accept', 'application/json'); | |
xhr.onload = function() { | |
if(xhr.status !== 200) { | |
reject(xhr.status); | |
return; | |
} | |
var response = xhr.response; | |
var code = response.statusCode.code; | |
switch(code) { | |
case '200': | |
resolve(response.contextElement); | |
break; | |
case '404': | |
resolve(null); | |
break; | |
default: | |
reject(code); | |
} | |
} | |
xhr.onerror = reject; | |
xhr.send(); | |
}); | |
} | |
function doQuery(entityId) { | |
queryContext(entityId).then(function(data) { | |
if(data) { | |
console.log('Data has been found: ', JSON.stringify(data)); | |
} | |
else { | |
console.log('Entity not found!'); | |
} | |
}).catch(function(err) { | |
console.error('Error while querying: ', err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment