Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.