Created
June 20, 2013 07:29
-
-
Save logicminds/5820860 to your computer and use it in GitHub Desktop.
This is a test to find out if we can get foreman data via an ajax call in the browser. However, I believe this is failing due to :
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control
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
<html> | |
<head> | |
<script> | |
var xmlhttp = null; | |
var jsondata = null; | |
function getFactData(hostname, factname, handler) | |
{ | |
var foreman="http://foreman/api"; | |
var factpath="fact_values?search=host=" + hostname + "+and+fact=" + factname; | |
var url = foreman + "/" + factpath; | |
if (window.XMLHttpRequest) | |
{// code for IE7+, Firefox, Chrome, Opera, Safari | |
xmlhttp=new XMLHttpRequest(); | |
} | |
else | |
{// code for IE6, IE5 | |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
xmlhttp.open("GET",url,true); | |
xmlhttp.onreadystatechange = handler; | |
xmlhttp.send(); | |
} | |
function onStateChange() | |
{ | |
if (xmlhttp.readyState != 4) return; | |
if (xmlhttp.status != 200 && xmlhttp.status != 304) | |
{ | |
alert('HTTP error ' + xmlhttp.status); | |
} | |
else{ | |
jsondata = JSON.parse(xmlhttp.responseText); | |
} | |
} | |
function getFactAsTimeAgo(hostname, factname, element){ | |
getFactData(hostname, factname, onStateChange); | |
if (jsondata){ | |
//var time = jsondata[hostname].factname; | |
//var ago = timeago(jsondata); | |
// element.getElementById("myDiv").innerHTML= ago; | |
//element.innerHTML = ago; | |
alert(jsondata) | |
} | |
} | |
// Takes an ISO time and returns a string representing how | |
// long ago the date represents. | |
function timeago(time){ | |
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), | |
diff = (((new Date()).getTime() - date.getTime()) / 1000), | |
day_diff = Math.floor(diff / 86400); | |
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ) | |
return; | |
return day_diff == 0 && ( | |
diff < 60 && "just now" || | |
diff < 120 && "1 minute ago" || | |
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" || | |
diff < 7200 && "1 hour ago" || | |
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") || | |
day_diff == 1 && "Yesterday" || | |
day_diff < 7 && day_diff + " days ago" || | |
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"; | |
} | |
</script> | |
</head> | |
<body> | |
<button type="button" onclick="getFactAsTimeAgo('your_node_name', 'macaddress', this); return false;">Request data</button> | |
<div id="myDiv"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment