Hello, im trying to add a new Feature in my rest service using ajax, but i got the following error:
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 .
Solved! Go to Solution.
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); } }); }
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.
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);
}
});
}
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.
Yes, it is w/o geom.
I wouldn't think you'd have to, but what if you used JSON.stringify(data)?
Try adding an empty geometry too, like geometry: {}
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.
If my memory is correct, you should pass the token as a URL paramater, so at end of the URL ?token=TOKEN
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"}
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); } }); }