https://www.arcgis.com/sharing/rest/content/features/generate Error

1654
7
07-19-2021 01:48 PM
ChitraKrishnan
Occasional Contributor

Hi,

Can somebody help me why  https://www.arcgis.com/sharing/rest/content/features/generate    is returning a html instead of json response in the attached code.

getting error -  Unexpected token '<'

Thanks

Chitra

 

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor

Instead of providing code as an attachment, please add it using "Insert code sample".  It makes it much easier to read your code and help you.

https://community.esri.com/t5/community-help-documents/how-to-insert-code-in-your-post/ta-p/914552

0 Kudos
ChitraKrishnan
Occasional Contributor

Hi,

I have added the code through insertCode option. Thanks for letting me know.

Can someone help me why  https://www.arcgis.com/sharing/rest/content/features/generate    is returning a html instead of json response in the attached code.

getting error -  Unexpected token '<'

const fileName='';

document
    .getElementById("uploadForm")
    .addEventListener("change", (event) => {
               fileName = event.target.value.toLowerCase();
});			   

//filepath is from local drive: D:\GIS\tl_2020_39_tabblock20.zip
	
const portalUrl = "https://www.arcgis.com";
let name = fileName.split(".");
name = name[0].replace("c:\\fakepath\\", "");

            var params = {
                'name': name,
                'targetSR': this.map.spatialReference,
                'maxRecordCount': 1000,
                'enforceInputFileSizeLimit': true,
                'enforceOutputJsonSizeLimit': true
            };

            var extent = scaleUtils.getExtentForScale(this.map, 40000);
            var resolution = extent.getWidth() / this.map.width;
            params.generalize = true;
            params.maxAllowableOffset = resolution;
            params.reducePrecision = true;
            params.numberOfDigitsAfterDecimal = 0;


            var myContent = {
                'filetype': 'shapefile',
                'publishParameters': JSON.stringify(params),
                'f': 'json'                
            };

            console.log(document.getElementById("uploadForm"));

            esriRequest({
                url: portalUrl + "/sharing/rest/content/features/generate",
                query: myContent,
                form: document.getElementById("uploadForm"),
                handleAs: "json",
                load: lang.hitch(this, function (response) {
                    if (response.error) {
                        errorHandler(response.error);
                        return;
                    }

var layerName = response.featureCollection.layers[0].layerDefinition.name;                   
addShapefileToMap(response.featureCollection);
                }),
                error: lang.hitch(this, errorHandler)
            });


            function errorHandler(error) {
                console.log('error!!');
            }



            function addShapefileToMap(featureCollection) {
                let sourceGraphics = [];
                const layers = featureCollection.layers.map((layer) => {
         const graphics = layer.featureSet.features.map((feature) => {
                        return Graphic.fromJSON(feature);
                    });

                    sourceGraphics = sourceGraphics.concat(graphics);
                    const featureLayer = new FeatureLayer({
                        objectIdField: "FID",
                       source: graphics,
                    fields: layer.layerDefinition.fields.map((field) => {
                            return Field.fromJSON(field);
                        })
                    });
                    return featureLayer;
                });

                this.map.addMany(layers);
            };

 

Thanks

Chitra

 

0 Kudos
ReneRubalcava
Honored Contributor

is this in 3x or 4x?

I thought it was 3x because you are using handleAs, but passing a query. In 3x, it's content, not query.

But if you are using 4x, it's query, but you should use responseType, not handleAs.

0 Kudos
ChitraKrishnan
Occasional Contributor

we are using 3x. 

I modified the code as below and getting a different error. Is my code correct?

{"error":{"code":405,"messageCode":"GWM_0005","message":"Method not supported.","details":[]}}

            var layersRequest = esriRequest({
                url: portalUrl + "/sharing/rest/content/features/generate",
                content: { 'f': "json", 'filetype': 'shapefile', 'publishParameters': JSON.stringify(params) },
                handleAs: "json"
            });

            layersRequest.then(
                function (response) {
                    console.log("Success: ", response.layers);
                }, function (error) {
                    console.log("Error: ", error.message);
                });

 

0 Kudos
ReneRubalcava
Honored Contributor

The only difference I can see at a glance from your code and the sample is the sample adds a callback.html parameter.

https://developers.arcgis.com/javascript/3/jssamples/portal_addshapefile.html

When you add the form, I think it should automatically make a POST request. Maybe verify that as well.

0 Kudos
ChitraKrishnan
Occasional Contributor

I copied the sample code from https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=layers-featurelayer-shap... to my local and it works for drp_county_boundary.zip. But when I try to load tl_2020_39_tabblock20.zip it throws below error

Index:1 Access to fetch at 'https://www.arcgis.com/sharing/rest/content/features/generate?filetype=shapefile&publishParameters=%...' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

 

Thanks

Chitra

0 Kudos
ChitraKrishnan
Occasional Contributor

I got the error code 405 resolved. I was referencing portalUrl as this.portalUrl. 

But again I am back to the same error, the generate returning a html instead of json response 

error -  Unexpected token '<'

<div>
    <div data-dojo-attach-point="mapNode"></div>

    <div>
        <form enctype="multipart/form-data" method="post" id="uploadForm">
            <div class="field">
                <label class="file-upload">
                    <span><strong>Add File</strong></span>
                    <input type="file" name="file" id="inFile" />
                </label>
            </div>
        </form>
    </div>


</div>

 

const fileName='';

document
    .getElementById("uploadForm")
    .addEventListener("change", (event) => {
               fileName = event.target.value.toLowerCase();
});			   

//filepath is from local drive: D:\GIS\tl_2020_39_tabblock20.zip
	
const portalUrl = "https://www.arcgis.com";
let name = fileName.split(".");
name = name[0].replace("c:\\fakepath\\", "");

 var params = {
                'name': name,
                'targetSR': this.map.spatialReference,
                'maxRecordCount': 1000,
                'enforceInputFileSizeLimit': true,
                'enforceOutputJsonSizeLimit': true
            };

            var extent = scaleUtils.getExtentForScale(this.map, 40000);
            var resolution = extent.getWidth() / this.map.width;
            params.generalize = true;
            params.maxAllowableOffset = resolution;
            params.reducePrecision = true;
            params.numberOfDigitsAfterDecimal = 0;

            var myContent = {
                'filetype': 'shapefile',
                'publishParameters': JSON.stringify(params),
                'f': 'json'                
            };


            console.log(document.getElementById("uploadForm"));
            console.log(dom.byId("uploadForm"));


            var layersRequest = esriRequest({
                url: portalUrl + "/sharing/rest/content/features/generate",                
                content: myContent,
                handleAs: "json",              
                form: document.getElementById("uploadForm")
            }, { usePost: 'true', });

            layersRequest.then(
                function (response) {
                    console.log("Success: ", response.layers);
                }, function (error) {
                    console.log("Error: ", error.message);
                });

 

0 Kudos