Select to view content in your preferred language

pull data function returning 400 Invalid URL

542
1
Jump to solution
09-10-2023 07:57 AM
MichaelKohler
Frequent Contributor

Hi,

I've been using the Damage Assessment Solutions that includes Survey123 Connect XLS forms. Part of that is the JavaScript functions that should pull data from a feature service and use the data to populate a field in the survey. I keep getting a 400 error saying the URL is invalid. I've even created a URL that I know works and short circuited the JavaScript code and still get the invalid URL error. I've had this working in other surveys, just not the damage assessment.

This is how the function is called from the XLS

pulldata("@javascript","myJSFunctions.js","queryPolygon",string(${location}),"PARCELID",pulldata("@property","token"),true)

 

Below is the code in the scripts folder. 

 

 

function queryPolygon(location,fields,token,debugmode){

    if (location===""){
        return (debugmode? "Location Object is empty":"");
    }

    var featureLayer = "https://services1.arcgis.com/**********/arcgis/rest/services/RE_PARCELS_WEB/FeatureServer/0";

    var coordsArray = location.split(" ");
    var coords = coordsArray[1] + "," + coordsArray[0]

    var xmlhttp = new XMLHttpRequest();
    var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"


    if (token){
        url = url + "&token=" + token;
    }

    xmlhttp.open("GET",url,false);
        xmlhttp.send();

    if (xmlhttp.status!==200){
        return (debugmode? xmlhttp.status:"");
    } else {
        var responseJSON=JSON.parse(xmlhttp.responseText)
        if (responseJSON.error){
            return (debugmode? JSON.stringify(responseJSON.error):"");
        } else {
            if (responseJSON.features[0]){
                return JSON.stringify(responseJSON.features[0]);
            }
            else{
                return (debugmode? "No Features Found":"");
            }
        }
    }
}

 

 

 This is the error that is stored in the JSON field. See Picture

{"code":400,"message":"Invalid URL","details":["Invalid URL"]}

Then I created a url that I know works and hardcoded it into the code and still get the same error.

 

 

function queryPolygon(location,fields,token,debugmode){
    if (location===""){
        return (debugmode? "Location Object is empty":"");
    }
    var featureLayer = "https://services1.arcgis.com/***********/arcgis/rest/services/RE_PARCELS_WEB/FeatureServer/0";
    var coordsArray = location.split(" ");
    var coords = coordsArray[1] + "," + coordsArray[0]
    var xmlhttp = new XMLHttpRequest();
    var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"

    //short circut url with known url that works when pasted into browser.
    var url = "https://services1.arcgis.com/**********/arcgis/rest/services/RE_PARCELS_WEB/FeatureServer/0/query?geometry=-80.39273,27.21473&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=PARCELID&returnGeometry=false&returnCount=1&f=json"

    if (token){
        url = url + "&token=" + token;
    }

    xmlhttp.open("GET",url,false);
        xmlhttp.send();

    if (xmlhttp.status!==200){
        return (debugmode? xmlhttp.status:"");
    } else {
        var responseJSON=JSON.parse(xmlhttp.responseText)
        if (responseJSON.error){
            return (debugmode? JSON.stringify(responseJSON.error):"");
        } else {
            if (responseJSON.features[0]){
                return JSON.stringify(responseJSON.features[0]);
            }
            else{
                return (debugmode? "No Features Found":"");
            }
        }
    }
}

 

 

 

Any ideas would be greatly appreciated. As well on what the debug mode is and how would someone debug this code.

 

0 Kudos
1 Solution

Accepted Solutions
MichaelKohler
Frequent Contributor

Good news, it's working. Bad news, I'm not sure what exactly happened.

What I think happened is that there is some sort of disconnect between the code in the script window and the js file in the scripts folder. What I ended up doing was l tried to launch the script in an editor and then made changes. It just seemed to flip a switch or something and then the survey worked as expected.

 

View solution in original post

0 Kudos
1 Reply
MichaelKohler
Frequent Contributor

Good news, it's working. Bad news, I'm not sure what exactly happened.

What I think happened is that there is some sort of disconnect between the code in the script window and the js file in the scripts folder. What I ended up doing was l tried to launch the script in an editor and then made changes. It just seemed to flip a switch or something and then the survey worked as expected.

 

0 Kudos