CORS Javascript API problem

5123
4
11-19-2015 10:09 AM
BrunoMendes
Occasional Contributor

I've implemented an example of FeatureLayer filtering using a Buffer geometry running from local disk and it worked like a charm out-of-the-box, here is a snippet of it:

function filterSegVia(geometry) {
  var query = new esri.tasks.Query();
  query.geometry = geometry;
  segViaLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
}

When I tried to load the map inside a JSP from a Java application running on JBoss server, I got this error message:

esri.config.defaults.io.proxyUrl não está configurado. Se fizer um pedido para um servidor CORS habilitado, mande o domínio para esri.config.defaults.io.corsEnabledServers

I tried to add the server domain and IP to 'corsEnabledServer' config as stated above but the same error occurred:

esri.config.defaults.io.corsEnabledServers.push("services-gis.prodam.am.gov.br");
esri.config.defaults.io.corsEnabledServers.push("10.20.1.245");

I did another test hosting my app in Mongoose, a very lightweight web server, and it worked. Here is the captured request from the API, header and data:

  1. POST /arcgis/rest/services/Dynamic/IdentificacaoSISP/FeatureServer/12/query HTTP/1.1 Host: services-gis.prodam.am.gov.br Connection: keep-alive Content-Length: 3294 Origin: http://localhost User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: */* Referer: http://localhost/featureLayerFiltering.html Accept-Encoding: gzip, deflate Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
  2. f=json&returnGeometry=true&spatialRel=esriSpatialRelIntersects&geometry=%7B%22rings%22%3A%5B%5B%5B-59.99709160681768%2C-3.000937507636297%5D%2C%5B-59.9970989883816%2C-3.00079665900631%5D%2C%5B-59.997121052199375%2C-3.000657353543388%5D%2C%5B-59.99715755653522%2C-3.000521117507333%5D%2C%5B-59.99720810143998%2C-3.000389443528667%5D%2C%5B-59.9972721331331%2C-3.0002637742550538%5D%2C%5B-59.99734895006996%2C-3.000145486545353%5D%2C%5B-59.99743771062813%2C-3.000035876384466%5D%2C%5B-59.997537442328344%2C-2.9999361446842525%5D%2C%5B-59.99764705248923%2C-2.99984738412609%5D%2C%5B-59.99776534019893%2C-2.9997705671892283%5D%2C%5B-59.99789100947254%2C-2.999706535496103%5D%2C%5B-59.99802268345121%2C-2.999655990591343%5D%2C%5B-59.99815891948726%2C-2.999619486255502%5D%2C%5B-59.99829822495018%2C-2.999597422437723%5D%2C%5B-59.99843907358017%2C-2.9995900408738105%5D%2C%5B-59.99857992221016%2C-2.999597422437723%5D%2C%5B-59.998719227673085%2C-2.999619486255502%5D%2C%5B-59.998855463709134%2C-2.999655990591343%5D%2C%5B-59.998987137687806%2C-2.999706535496103%5D%2C%5B-59.99911280696141%2C-2.9997705671892283%5D%2C%5B-59.99923109467112%2C-2.99984738412609%5D%2C%5B-59.999340704832%2C-2.9999361446842525%5D%2C%5B-59.99944043653222%2C-3.000035876384466%5D%2C%5B-59.99952919709038%2C-3.000145486545353%5D%2C%5B-59.99960601402724%2C-3.0002637742550538%5D%2C%5B-59.999670045720364%2C-3.000389443528667%5D%2C%5B-59.999720590625124%2C-3.000521117507333%5D%2C%5B-59.99975709496097%2C-3.000657353543388%5D%2C%5B-59.99977915877874%2C-3.00079665900631%5D%2C%5B-59.99978654034266%2C-3.000937507636297%5D%2C%5B-59.99977915877874%2C-3.0010783562662837%5D%2C%5B-59.99975709496097%2C-3.001217661729206%5D%2C%5B-59.999720590625124%2C-3.0013538977652607%5D%2C%5B-59.999670045720364%2C-3.001485571743927%5D%2C%5B-59.99960601402724%2C-3.00161124101754%5D%2C%5B-59.99952919709038%2C-3.0017295287272407%5D%2C%5B-59.99944043653222%2C-3.001839138888128%5D%2C%5B-59.999340704832%2C-3.0019388705883414%5D%2C%5B-59.99923109467112%2C-3.002027631146504%5D%2C%5B-59.99911280696141%2C-3.0021044480833656%5D%2C%5B-59.998987137687806%2C-3.0021684797764907%5D%2C%5B-59.998855463709134%2C-3.002219024681251%5D%2C%5B-59.998719227673085%2C-3.002255529017092%5D%2C%5B-59.99857992221016%2C-3.002277592834871%5D%2C%5B-59.99843907358017%2C-3.0022849743987834%5D%2C%5B-59.99829822495018%2C-3.002277592834871%5D%2C%5B-59.99815891948726%2C-3.002255529017092%5D%2C%5B-59.99802268345121%2C-3.002219024681251%5D%2C%5B-59.99789100947254%2C-3.0021684797764907%5D%2C%5B-59.99776534019893%2C-3.0021044480833656%5D%2C%5B-59.99764705248923%2C-3.002027631146504%5D%2C%5B-59.997537442328344%2C-3.0019388705883414%5D%2C%5B-59.99743771062813%2C-3.001839138888128%5D%2C%5B-59.99734895006996%2C-3.0017295287272407%5D%2C%5B-59.9972721331331%2C-3.00161124101754%5D%2C%5B-59.99720810143998%2C-3.001485571743927%5D%2C%5B-59.99715755653522%2C-3.0013538977652607%5D%2C%5B-59.997121052199375%2C-3.001217661729206%5D%2C%5B-59.9970989883816%2C-3.0010783562662837%5D%2C%5B-59.99709160681768%2C-3.000937507636297%5D%2C%5B-59.99709160681768%2C-3.000937507636297%5D%5D%5D%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%2C%22latestWkid%22%3A4326%7D%7D&geometryType=esriGeometryPolygon&inSR=4326&outFields=OBJECTID%2CANNOT_ANTERIOR%2CANNOT_ATUAL%2CANNOT_POPULAR&outSR=4326

As a final test, I've created a POST request manually using the following script in order to make the same request from the map loaded from Jboss:

var data = new FormData()
data.append('f','json')
data.append('returnGeometry','true')
data.append('spatialRel','esriSpatialRelIntersects')
data.append('geometryType','esriGeometryPolygon')
data.append('inSR','4326')
data.append('outSR','4326')
data.append('outFields','OBJECTID%2CANNOT_ANTERIOR%2CANNOT_ATUAL%2CANNOT_POPULAR')
data.append('geometry','%7B%22rings%22%3A%5B%5B%5B-59.99709160681768%2C-3.000937507636297%5D%2C%5B-59.9970989883816%2C-3.00079665900631%5D%2C%5B-59.997121052199375%2C-3.000657353543388%5D%2C%5B-59.99715755653522%2C-3.000521117507333%5D%2C%5B-59.99720810143998%2C-3.000389443528667%5D%2C%5B-59.9972721331331%2C-3.0002637742550538%5D%2C%5B-59.99734895006996%2C-3.000145486545353%5D%2C%5B-59.99743771062813%2C-3.000035876384466%5D%2C%5B-59.997537442328344%2C-2.9999361446842525%5D%2C%5B-59.99764705248923%2C-2.99984738412609%5D%2C%5B-59.99776534019893%2C-2.997705671892283%5D%2C%5B-59.99789100947254%2C-2.999706535496103%5D%2C%5B-59.99802268345121%2C-2.999655990591343%5D%2C%5B-59.99815891948726%2C-2.999619486255502%5D%2C%5B-59.99829822495018%2C-2.999597422437723%5D%2C%5B-59.99843907358017%2C-2.9995900408738105%5D%2C%5B-59.99857992221016%2C-2.999597422437723%5D%2C%5B-59.998719227673085%2C-2.999619486255502%5D%2C%5B-59.998855463709134%2C-2.999655990591343%5D%2C%5B-59.998987137687806%2C-2.999706535496103%5D%2C%5B-59.99911280696141%2C-2.9997705671892283%5D%2C%5B-59.99923109467112%2C-2.99984738412609%5D%2C%5B-59.999340704832%2C-2.9999361446842525%5D%2C%5B-59.99944043653222%2C-3.000035876384466%5D%2C%5B-59.99952919709038%2C-3.000145486545353%5D%2C%5B-59.99960601402724%2C-3.0002637742550538%5D%2C%5B-59.999670045720364%2C-3.000389443528667%5D%2C%5B-59.999720590625124%2C-3.000521117507333%5D%2C%5B-59.99975709496097%2C-3.000657353543388%5D%2C%5B-59.99977915877874%2C-3.00079665900631%5D%2C%5B-59.99978654034266%2C-3.000937507636297%5D%2C%5B-59.99977915877874%2C-3.0010783562662837%5D%2C%5B-59.99975709496097%2C-3.001217661729206%5D%2C%5B-59.999720590625124%2C-3.0013538977652607%5D%2C%5B-59.999670045720364%2C-3.001485571743927%5D%2C%5B-59.99960601402724%2C-3.00161124101754%5D%2C%5B-59.99952919709038%2C-3.0017295287272407%5D%2C%5B-59.99944043653222%2C-3.001839138888128%5D%2C%5B-59.999340704832%2C-3.0019388705883414%5D%2C%5B-59.99923109467112%2C-3.002027631146504%5D%2C%5B-59.99911280696141%2C-3.0021044480833656%5D%2C%5B-59.998987137687806%2C-3.0021684797764907%5D%2C%5B-59.998855463709134%2C-3.002219024681251%5D%2C%5B-59.998719227673085%2C-3.002255529017092%5D%2C%5B-59.99857992221016%2C-3.002277592834871%5D%2C%5B-59.99843907358017%2C-3.0022849743987834%5D%2C%5B-59.99829822495018%2C-3.002277592834871%5D%2C%5B-59.99815891948726%2C-3.002255529017092%5D%2C%5B-59.99802268345121%2C-3.002219024681251%5D%2C%5B-59.99789100947254%2C-3.0021684797764907%5D%2C%5B-59.99776534019893%2C-3.0021044480833656%5D%2C%5B-59.99764705248923%2C-3.002027631146504%5D%2C%5B-59.997537442328344%2C-3.0019388705883414%5D%2C%5B-59.99743771062813%2C-3.001839138888128%5D%2C%5B-59.99734895006996%2C-3.0017295287272407%5D%2C%5B-59.9972721331331%2C-3.00161124101754%5D%2C%5B-59.99720810143998%2C-3.001485571743927%5D%2C%5B-59.99715755653522%2C-3.0013538977652607%5D%2C%5B-59.997121052199375%2C-3.001217661729206%5D%2C%5B-59.9970989883816%2C-3.0010783562662837%5D%2C%5B-59.99709160681768%2C-3.000937507636297%5D%2C%5B-59.99709160681768%2C-3.000937507636297%5D%5D%5D%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%2C%22latestWkid%22%3A4326%7D%7D')


var request = new XMLHttpRequest()
request.open('POST', 'http://10.20.1.153/arcgis/rest/services/Dynamic/IdentificacaoSISP/FeatureServer/12')
request.onreadystatechange = function () {
  if (this.status == 200 && this.readyState == 4) {
    console.log('response: ' + this.responseText)
  }
}
request.send(data)

And it worked. No CORS error.

So, how to solve this? Does the fact that the app is running on JBoss interfere with the ArcGIS Javascript API?

Maybe a bug from the API?

Tags (2)
0 Kudos
4 Replies
ChrisSmith7
Frequent Contributor

Just curious, is CORS enabled in JBoss?

Chapter 11. CORS

BrunoMendes
Occasional Contributor

That link is about JBoss Keycloak, a Single Sign-On solution.
I'm using JBoss Application Server version 5.1 GA, it has no CORS configuration according to its documentation: JBoss Application Server Official Documentation... | JBoss Developer .

0 Kudos
ChrisSmith7
Frequent Contributor

I am not really familiar with JBoss; however, when I set-up server over IIS a few years back, I had quite a few problems with CORS and the proxy server. Some of the literature I came across (for multiple server flavors) indicated a config setting would need to be adjusted.

For the version of IIS I am using, I did not need to make a change in the manager, I just ended up making changes to config files in the codebase - that solved my problem, e.g. enable cross-origin resource sharing

I am really not sure on your end - I mentioned a potential config issue because, you say, it works in Mongoose. I am grasping at straws for you - I don't have any experience with JBoss, but here are some resources to maybe give a once-over, just in case:

http://stackoverflow.com/questions/26610657/how-to-enable-cors-in-jboss

http://stackoverflow.com/questions/31083854/how-to-enable-cors-with-a-java-restful-web-service-on-jb...

Don't want to send you down the wrong path, but there's really not a lot out there with JBoss/ArcGIS Server/CORS

BrunoMendes
Occasional Contributor

Correct me if I'm wrong, but I would need to enable CORS in my Java app in case clients from a different domain would request something from it, like the second post you linked, where he needed to enable CORS to provide web services to clients from another app running on a different domain.

This is not my case, the user is accessing my Java app, but then he needs to consume services from my ArcGIS Server Site, so the CORS needs to be configured in the Web Adaptor.

As far as I know, it is already configured in my Web Adaptor, otherwise the javascript test would not run successfully.

0 Kudos