Using applyedits with Ajax

4152
9
Jump to solution
04-14-2016 08:24 AM
EvelynHernandez
Occasional Contributor III


Hello, im trying to add a new Feature in my rest service using ajax, but i got the following error:

  1. Object
    1. code:500
    2. details:Array[1]
      1. 0:"No edits ('adds', 'updates' or 'deletes') were specified."
      2. length:1
      3. __proto__:Array[0]
    3. message:"Unable to complete operation."

What am i doing wrong?

//Example url: http://myserverarcgis/arcgis/rest/services/mylayer/FeatureServer/1/applyEdits 
//My attributes from my layer are: user,from and module.

function saveLogin(){
  var arrayOfAttributes = [
    { "adds":[
      {
        "attributes" : {
          "user" : "Evelyn4",
          "from" : "Evelyn4",
          "module" : "Evelyn4"
        }
      }
    ]
  }
  ];

  var url = myLayers.write_logAccess();

  jQuery.ajax({
    type: 'POST',
    url: url,
    data: arrayOfAttributes,
    dataType:'json',
    success: (success) => {
      console.log(success);
    },
    error: (error) => {
      console.log(error);
    }
  });
}

Thanks for ur help .

0 Kudos
1 Solution

Accepted Solutions
EvelynHernandez
Occasional Contributor III

I solved my problem.

function saveLogin(user,page,module){


  const data = {
    f: 'json',
    adds: JSON.stringify([{ attributes: { "user": user, "page": page, "module": module  }, geometry: {} }])
  };


  jQuery.ajax({
    method: 'POST',
    url: myLayers.write_logAccess(),
    data: data,
    dataType:'json',
    success: (success) => {
      console.log(success);

    },
    error: (error) => {
      console.log(error);
    
    }
  });
}

View solution in original post

9 Replies
RobertWinterbottom
Occasional Contributor

If you look in the chrome developer tools network tab, what do you see under form data.  It looks like you may not have the right format for form data.  I think it should be an object not an array, something like this:

data = {
  f: 'json',
  adds: [{
    attributes: {
      "user": "",
      "objectid: "" // This may be necessary for it to know what feature to update
    }
  }]
};

And then use this in your AJAX request instead of arrayOfAttributes

jQuery.ajax({
   ... // Your stuff here
  data: data
});

That error means the service could not infer what you are trying to do meaning it did not see the 'adds' key in your post data.

0 Kudos
EvelynHernandez
Occasional Contributor III

I got this and still the same error.

function saveLogin(){

  var data = {

  f: 'json',

  adds: [{

    attributes: {

      "user": "Evelyn"

    }

  }]

};

  var url = myLayers.write_logAccess();

  jQuery.ajax({

    type: 'POST',

    url: url,

    data: data,

    dataType:'json',

    success: (success) => {

      console.log(success);

    },

    error: (error) => {

      console.log(error);

    }

  });

}

0 Kudos
ReneRubalcava
Frequent Contributor

Roberts answer to send an object is correct. Is this a featureservice without geometries? I notice you're not sending any.

You can look at the REST API here.

ArcGIS REST API

0 Kudos
EvelynHernandez
Occasional Contributor III

Yes, it is w/o geom.

0 Kudos
ReneRubalcava
Frequent Contributor

I wouldn't think you'd have to, but what if you used JSON.stringify(data)?

Try adding an empty geometry too, like geometry: {}

0 Kudos
EvelynHernandez
Occasional Contributor III

i got this as error:

Object {readyState: 4, responseText: "<html>

↵<head>

↵<title>ArcGIS Server REST API Logi…

↵</form>

↵</div>

↵<br/><br/>

↵</body>

↵</html>

↵", status: 200, statusText: "OK"}

I have the following code:

function saveLogin(){

  var data = {

  token: token.read(),

  f: 'json',

  adds: [{

    attributes: {

      "usuario": "Evelyn"

    }

  }]

};

  jQuery.ajax({

    type: 'POST',

    url: "http://myserver/arcgis/rest/services/mylayer/LogAccess/FeatureServer/1/applyEdits",

    data: JSON.stringify(data),

    dataType:'json',

    success: (success) => {

      console.log(success);

      console.log("pase");

    },

    error: (error) => {

      console.log(error);

      console.log("no pase");

    }

  });

}

Now i pass the token through the data.

0 Kudos
ReneRubalcava
Frequent Contributor

If my memory is correct, you should pass the token as a URL paramater, so at end of the URL ?token=TOKEN

0 Kudos
EvelynHernandez
Occasional Contributor III

yes, for example im using this structure for accessing to my layers (for example when i want to add a new layer and it works perfectly):

write_logAccess(){  /*using*/
        return serviceURL + "Admin/LogAccesos/FeatureServer/1?f=json&token=" + token.read();
    }

where token.read() is the token saved in local storage var previously.

But how im using the applyedits, im not sure how to put the url... could be like this?

  return serviceURL + "Admin/LogAccesos/FeatureServer/1/applyEdits?f=json&token=" + token.read();

so my code for save the user is the following now:

function saveLogin(){
  var data = {
  f: 'json',
  adds: [{
    attributes: {
      "user": "Evelyn3"
    },
    geometry: {}
  }]
};

  jQuery.ajax({
    type: 'POST',
    url: myLayers.write_logAccess(),
    data: JSON.stringify(data),
    dataType:'json',
    success: (success) => {
      console.log(success);
      console.log("pase");
    },
    error: (error) => {
      console.log(error);
      console.log("no pase");
    }
  });
}

and i still get this error: what could be happening?

Object {readyState: 4, status: 404, statusText: "error"}

0 Kudos
EvelynHernandez
Occasional Contributor III

I solved my problem.

function saveLogin(user,page,module){


  const data = {
    f: 'json',
    adds: JSON.stringify([{ attributes: { "user": user, "page": page, "module": module  }, geometry: {} }])
  };


  jQuery.ajax({
    method: 'POST',
    url: myLayers.write_logAccess(),
    data: data,
    dataType:'json',
    success: (success) => {
      console.log(success);

    },
    error: (error) => {
      console.log(error);
    
    }
  });
}