I would like to add records to a table using REST API. I followed Robert Scheitlin, GISP's example to retrieve a table here, and modified the URL with the following one:
"http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/1/addFeatures?features=[{'attributes' : {'agree_with_incident' : '2', 'notes' : 'my notes..'}}]&f=json";
based on Domenico Ciavarella's answer in this post. However, I am getting this error in the console when I try to execute the request: "dojo.io.script error Error: Unable to complete operation."
I am very new to the REST API, and I don't see many examples. Here is my entire code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Add a Record</title>
<style>
td {
padding: 4px;
}
#loading {
visibility: hidden;
}
</style>
<script src="http://js.arcgis.com/4.2/"></script>
<script>
require([
"esri/request",
"esri/config",
"dojo/json",
"dojo/dom",
"dojo/on",
"dojo/domReady!"
], function(esriRequest, esriConfig, JSON, dom, on) {
esriConfig.request.proxyUrl = "http://gislap183/js/proxy/proxy.ashx";
function doRequest(){
var url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/1...[{'attributes' : {'agree_with_incident' : '2', 'notes' : 'my notes..'}}]&f=json";
esriRequest(url, {
responseType: "json",
callbackParamName: "callback"
}).then(function(response){
showResults(response);
});
}
var resultsTable = dom.byId("tbl");
// Executes when the promise from find.execute() resolves
function showResults(response) {
var results = response.data;
// Clear the cells and rows of the table to make room for new results
resultsTable.innerHTML = JSON.stringify(results);
}
// Executes each time the promise from find.execute() is rejected.
function rejectedPromise(err) {
console.error("Promise didn't resolve: ", err.message);
}
// Run doRequest() when button is clicked
on(dom.byId("execBtn"), "click", doRequest);
});
</script>
</head>
<body>
<input type="button" value="Execute" id="execBtn" />
<br>
<br>
<table id="tbl"></table>
</body>
</html>
How can I fix this? Thanks.
Solved! Go to Solution.
Naci,
Here is the code working. Make sure you change the proxy url to your proxy url or it will not work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Add a Record</title>
<style>
td {
padding: 4px;
}
#loading {
visibility: hidden;
}
</style>
<script src="http://js.arcgis.com/4.3/"></script>
<script>
require([
"esri/request",
"esri/config",
"esri/core/urlUtils",
"dojo/json",
"dojo/dom",
"dojo/on",
"dojo/domReady!"
], function(esriRequest, esriConfig, urlUtils, JSON, dom, on) {
esriConfig.request.proxyUrl = "http://gislap183/js/proxy/proxy.ashx";
function doRequest(){
var url = urlUtils.urlToObject("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/1...[{'attributes':{'agree_with_incident':2,'notes':'problemo grande1'}}]&f=json");
esriRequest(url.path, {
responseType: "json",
query: url.query,
useProxy: true,
callbackParamName: "callback",
method: "post"
}).then(function(response){
showResults(response);
}).otherwise(rejectedPromise);
}
var resultsTable = dom.byId("tbl");
// Executes when the promise from find.execute() resolves
function showResults(response) {
var results = response.data;
// Clear the cells and rows of the table to make room for new results
resultsTable.innerHTML = JSON.stringify(results);
}
// Executes each time the promise from find.execute() is rejected.
function rejectedPromise(err) {
console.error("Promise didn't resolve: ", err.message);
}
// Run doRequest() when button is clicked
on(dom.byId("execBtn"), "click", doRequest);
});
</script>
</head>
<body>
<input type="button" value="Execute" id="execBtn" />
<br>
<br>
<table id="tbl"></table>
</body>
</html>
Naci,
Here is the code working. Make sure you change the proxy url to your proxy url or it will not work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Add a Record</title>
<style>
td {
padding: 4px;
}
#loading {
visibility: hidden;
}
</style>
<script src="http://js.arcgis.com/4.3/"></script>
<script>
require([
"esri/request",
"esri/config",
"esri/core/urlUtils",
"dojo/json",
"dojo/dom",
"dojo/on",
"dojo/domReady!"
], function(esriRequest, esriConfig, urlUtils, JSON, dom, on) {
esriConfig.request.proxyUrl = "http://gislap183/js/proxy/proxy.ashx";
function doRequest(){
var url = urlUtils.urlToObject("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/1...[{'attributes':{'agree_with_incident':2,'notes':'problemo grande1'}}]&f=json");
esriRequest(url.path, {
responseType: "json",
query: url.query,
useProxy: true,
callbackParamName: "callback",
method: "post"
}).then(function(response){
showResults(response);
}).otherwise(rejectedPromise);
}
var resultsTable = dom.byId("tbl");
// Executes when the promise from find.execute() resolves
function showResults(response) {
var results = response.data;
// Clear the cells and rows of the table to make room for new results
resultsTable.innerHTML = JSON.stringify(results);
}
// Executes each time the promise from find.execute() is rejected.
function rejectedPromise(err) {
console.error("Promise didn't resolve: ", err.message);
}
// Run doRequest() when button is clicked
on(dom.byId("execBtn"), "click", doRequest);
});
</script>
</head>
<body>
<input type="button" value="Execute" id="execBtn" />
<br>
<br>
<table id="tbl"></table>
</body>
</html>
Thank you very much, Robert. However, when I try to execute the query, I get the following errors in the console:
And when I check the records I see that my records are not added. I can see that you were able to add records yesterday. So I wonder why mine is not working.
Now I should tell that I used whatever proxy address you mentioned. I don't know anything about proxies, should I have made my own proxy?
Naci,
Yes , I mentioned that you would have to change the proxy url to your own proxy. I guess you have to start learning about proxies then.
GitHub - Esri/resource-proxy: Proxy files for DotNet, Java and PHP.
Thank you very much!