How to fix CORS issues when consuming AGS map services with JSAPI for ArcGIS?

2787
2
04-03-2019 03:11 PM
Arne_Gelfert
Regular Contributor

I am working on my local machine with JSAPI in VS Code, and I'm getting the following CORS related error thrown at me in Chrome Dev Tools:

Access to XMLHttpRequest at 'https//... my environment ..../MapServer?f=json' 
from origin 'http://localhost:8080' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.‍‍‍‍‍

I've read enough about CORS to understand its purpose, and that according to ESRI, ...

By default, ArcGIS Server allows cross-domain requests so Javascript clients can invoke the server's services from any domain.... (Restrict cross-domain requests to ArcGIS Server—ArcGIS Server Administration (Linux) | ArcGIS Enterp...)

So I think no additional tweaks to the web server are warranted. I've been able to "work around" this by developing in a directory that's on the same Dev/Test box where my map services are running on. This works great. Additionally, I've reviewed 'Allowed Origins' in ArcGIS Server Administrator to make it meets my needs.

Are there other adjustments I could make that would allow me to still do all my work locally and hit AGS map services without CORS issues? I get the sense from reading various threads online that a lot of folks new to working with JSAPI may be running into this. But I haven't found the ESRI documentation tremendously illuminating.

2 Replies
PatrickLadd
Occasional Contributor

Did you ever resolve this issue using localhost?  I just ran into this issue this morning (4/19/21) out of the blue - yet it worked fine for the previous 17 years.   I can run a development sample on the same server (or any server) but I can't run it through localhost:44300.  And it's only the web page with the map services - all other pages load and work correctly.

0 Kudos
PatrickLadd
Occasional Contributor

NOW I remember!  It was a self-inflicted wound.  For anyone else experiencing this, maybe you did what I did and this will help you too.  On the recommendation of  ESRI on this page,  I removed the wild card from Allowed Origins on my servicesDirectory settings in ArcGISSerer Admin and replaced it with my two GIS app server URL - this means that only this server is allowed to send Javascript requests to ArcGIS Server on my GIS data server.  It also means that the localhost machine (my development PC) cannot send javascript requests to ArcGIS server.  Once I added https://localhost to the Services Directory > AllowedOrigins it started working again.

0 Kudos