Trying to return the date value from a URL via getJSON for the id='alert'. Seems like the getJSON is not working correctly even with async set to False. I took this code from another script that worked correctly.
<!DOCTYPE html>
<html>
<body>
<p><input type="text" id="input" value="100 | 47.6735" rows="30" cols="50"></p>
<button class="btn btn-primary btn-lg" onclick="getAlert()">Get Date Alert</button>
<p id="alert"></p>
<script>
$.ajaxSetup({
async: false
});
function getJSON_data(url){
var date = '1'
$.getJSON(
url,
function(data) {
var date = data.date
});
return {'test':date}
}
function getAlert() {
var str = document.getElementById("input").value;;
var res = str.split(" | ");
var id = res[0];
var url = 'http://date.jsontest.com/'
var alert = getJSON_data(url).test;
document.getElementById("alert").innerHTML = alert;
}
</script>
</body>
</html>
Added Async and Promises to the js portion of the script. Can't quite get the date to return in the "alert" portion.
...
function getJSON_data(url){
return new Promise((resolve, reject) => {
$.getJSON(
url,
function({date}) {
resolve({test: date})
});
})
})
}
function getAlert() {
var str = document.getElementById("input").value;
var res = str.split(" | ");
var id = res[0];
var url = 'http://date.jsontest.com/';
var alert = (await getJSON_data(url)).test;
document.getElementById("alert").innerHTML = alert
}
...