Select to view content in your preferred language

How to - addFeatures operation in ArcGIS Server REST API using JavaScript and jQuery

13060
8
07-24-2013 08:53 AM
ClintCabanero
Frequent Contributor
Hello all, thanks for your time.

Context:
I have an ArcGIS Server 10.1 Enterprise Advanced instance on Windows 2008 Server R2. 
I have data in an Enterprise SDE Geodatabase on SQL Server.
I have successfully published a Feature Service with editing capabilities.  I confirm that I can edit the feature service because when I go to http://myserver/arcgis/rest/services/myservice/FeatureServer/0/addFeatures,  I can successfully add a feature by typing in an array of json objects into the 'features' text box of the Esri REST API web app.

What I'm Having Trouble Doing:
I now want to execute the addFeatures operation against my FeatureService using the REST API - but do it programmatically with JavaScript and jQuery. 

Here is my code ...

var theData = [
            {
                "geometry": {
                    "x": -13316097.345,
                    "y": 5861939.546999998
                },
                "attributes": {
                    "Column1": "Value1",
                    "Column2": "Value2"
                }
            }
        ];


       $.ajax({
            url: "http://myhostname/arcgis/rest/services/myservice/FeatureServer/0/addFeatures",
            dataType: 'json',
            type: 'POST',
            data: theData,
            success: function (data, textStatus, jqXHR) {

                //
                console.log('success');
                console.log(data);
                console.log(textStatus);
                console.log(jqXHR)
            },
            error: function (jqXHR, textStatus, errorThrown) {

                //
                console.log('error'); //<-- the response lands here ...
                console.log(jqXHR); //<-- the console logs the object
                console.log(textStatus); //<-- the console logs 'parsererror'
                console.log(errorThrown); //< -- the console logs 'SyntaxError {}'
            },
            complete: function (jqXHR, textStatus) {

                //
                console.log('complete');
                console.log(jqXHR);
                console.log(textStatus);
            }
        });


    
As you can see, the response I get from the GIS server is 'parse error'.  I believe this to potentially mean that the data that I am sending with my POST request is malformed? 

Questions:
1.What is the proper format/structure for the data in my POST request to my editable feature service?  Is my data structure wrong?  This is the same structure used when manually entering into the Esri REST API out-of-the-box web app.
2. Anyone else see anything incorrect about this $.ajax() call?

Thank you!!!
0 Kudos
8 Replies
ReneRubalcava
Honored Contributor
Edit - this is a better answer, because my brain stopped working earlier.

It is addFeatures, I was looking at it wrong
http://resources.arcgis.com/en/help/rest/apiref/fsadd.html

Your format looks good, but when using $.ajax I think you have to stringify the data before sending it.
So
data: JSON.stringify(theData)
0 Kudos
ClintCabanero
Frequent Contributor
Thanks for the reply odoe.  I've tried several variations of this and it is still not working.  Anyone else have any other ideas?

Thank you
0 Kudos
KellyHutchins
Esri Frequent Contributor
Is there a reason why you don't want to use the JSAPI to do this? You can use the feature layer's applyEdits method:

https://developers.arcgis.com/en/javascript/jsapi/featurelayer.html#applyedits
0 Kudos
ClintCabanero
Frequent Contributor
Thanks for the reply Kelly.  The requirement is to use the ArcGIS Server REST API and JavaScript - since the interface won't have a map with a feature layer.  Have you had success executing the addFeatures operation using the REST API, JavaScript with either jQuery or Dojo AJAX patterns?

Thank you
0 Kudos
FarhadNavaei
Regular Contributor

I don't know if you have found any solution for your question yet, but in case you are still interested, you can test the following:

var theData = {"features": [

            {

                "geometry": {

                    "x": -13316097.345,

                    "y": 5861939.546999998

                },

                "attributes": {

                    "Column1": "Value1",

                    "Column2": "Value2"

                }

            }

        ]};

another way that I'd recommend, and actually is worked for me, is to add the "features" parameter at the end of the url string:

          var theData = [{    "geometry": {    "x": -13316097.345,    "y": 5861939.546999998    },

               "attributes": {    "Column1": "Value1",    "Column2": "Value2"    }

          } ];

          $.ajax({ 

               url: "http://myhostname/arcgis/rest/services/myservice/FeatureServer/0/addFeatures" + JSON.stringify(theData)

               , dataType: 'html' // the datatype must be html

               , type: 'POST'

               //, data: theData  - remove this part

               , success: function (data, textStatus, jqXHR) {

               ......

AndrzejBieniek
Occasional Contributor

Make sure you specify the f and features parameters in the post body (The url has been changed in the code below, so do not try to use it):

$scope.addFeature= function(){

   

     var url = "http://services6.arcgis.com/dD0xfCNJ6/arcgis/rest/services/US/FeatureServer/0/addFeatures";

   

    var newFeature = {

            "geometry" : {"x" : -122.679186, "y" : 45.539699}, 

            "attributes" : {

                  "CandidateName" : "Hillary Clinton"

            },

          "spatialReference" : {

          "wkid" : "4326"

       }

  };

  var features = [];

  features.push(newFeature);

   

     var featuresString = JSON.stringify(features);

  data = "f=json&features="+featuresString;

  var config={

              headers : {

                'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'

            }};

   

    $http.post(url, data, config)

    .then(

    function(response){

  console.log(response);

    },

    function(response){

  console.log(response);

    }

  );

}

satbirsingh
Emerging Contributor

Thanks Andrzej !!

I was having the same issue and tried to append f=json but that did not worked.

Once I added "f=json&features=" as per your suggestion/post it worked !!

GuusClaessen
New Contributor

For anyone encountering this with jquery.post(), make sure that you JSON.stringify the features part. So for example, this works:

var url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Notes/FeatureServer/0/addFeatures";

var feature = {
         "geometry" : {"x" : 2, "y" : 52},
          "attributes" : {
                "note" : "Hello!",
                "name": "Guus"
           },
           "spatialReference" : {
                    "wkid" : "4326"
            }
};

$.post(url, {

          features: JSON.stringify([feature]),

          f: "json"

})
.done(function(results) {
        console.log(results);
})
.fail(function(error) {
        console.log(error);
});

(note that you have to put the one feature in an array) 

I hope this will help!

0 Kudos