I have this javascript which has worked for years, and then as of recent doesn't work most of the time and causes the surveys in the field app to load very slowly. Has anyone else had issues with javascript in their surveys recently?
// Query the Open Weather API
// https://openweathermap.org/api
// How to get started with the API: https://openweathermap.org/appid
function runWeatherCalcs(lat, lon, time) {
// Check to make sure we have latitude, longitude and an API key
if (lat == null || lon == null || time == null) {
return "";
}
// Create the request object
var xmlhttp = new XMLHttpRequest();
// Format the URL with the input parameters
var lat_param = "lat=" + lat;
var lon_param = "lon=" + lon;
var time_param = "dt=" + time;
var key_param = "appid=********************";
var format_param = "format=json";
var units_param = "units=imperial";
var parameters = [lat_param, lon_param, time_param, key_param, format_param, units_param].join("&");
var url = "https://api.openweathermap.org/data/3.0/onecall/timemachine?" + parameters;
// Make the request
xmlhttp.open("GET", url, false); // synchronous
xmlhttp.send();
// Check the response. 200 indicates success from the API
if (xmlhttp.status != 200) {
return "";
} else {
var responseJSON = JSON.parse(xmlhttp.responseText);
if (responseJSON.error) {
return "";
} else {
if (responseJSON) {
return JSON.stringify(responseJSON);
} else {
return "";
}
}
}
}
Have you validated that your query is not failing/timing out? There is no error handling apparent here --
// Check the response. 200 indicates success from the API if (xmlhttp.status != 200) { return "";
Also, might try executing the query with Postman to validate your expectations.
Yep, I've seen similar issues lately. Survey123's newer versions don't handle synchronous JavaScript (XMLHttpRequest with false) well anymore — it can cause lag or even break the form, especially on mobile.
Your weather script is making a blocking call, which freezes the app while it waits. I’d suggest moving the API call outside the survey (like using Power Automate or a webhook), or rewriting it with async logic — though async can be tricky in Survey123.
Looks like Esri tightened things up in the latest updates. You're not alone!