Skip to content

Instantly share code, notes, and snippets.

@jmcanterafonseca
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcanterafonseca/944855e22e7a1466b867 to your computer and use it in GitHub Desktop.
Save jmcanterafonseca/944855e22e7a1466b867 to your computer and use it in GitHub Desktop.
FIWARE - Query Context Data from Web Browser
// 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