ArcGIS JS API v 4.x Cors

2122
5
Jump to solution
02-14-2020 05:45 AM
AbdelrahmanAbdelrazek
New Contributor III

Hello everyone, 

We have a cached map service, whenever I try to add it to the map using ArcGIS JS API v 3.x "ArcGISTiledMapServiceLayer" it works fine.  

But whenever I try to add it to the map, using ArcGIS JS API v 4.x "TileLayer" I get this error 

"Access to fetch at 'Service URL' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

Could anyone please assist me with that.

Thanks in advance.

Kind regards,

Abdelrahman.

0 Kudos
1 Solution

Accepted Solutions
RhysDonoghue2
New Contributor III

hi Abdelrahman,

You may need to add a trusted server, e.g. 

config | ArcGIS API for JavaScript 4.14 

require(["esri/config"], function(esriConfig) { esriConfig.request.trustedServers.push("[<protocol>//]<hostname>.<domain>[:<port>]"); });

Regards,

Rhys

View solution in original post

0 Kudos
5 Replies
RhysDonoghue2
New Contributor III

hi Abdelrahman,

You may need to add a trusted server, e.g. 

config | ArcGIS API for JavaScript 4.14 

require(["esri/config"], function(esriConfig) { esriConfig.request.trustedServers.push("[<protocol>//]<hostname>.<domain>[:<port>]"); });

Regards,

Rhys

View solution in original post

0 Kudos
AbdelrahmanAbdelrazek
New Contributor III

Thanks Rhys for your response, I did try that.

But unfortunately still getting the same error.

0 Kudos
AbdelrahmanAbdelrazek
New Contributor III

This acatually worked for me when I switched to 4.15

Thanks Rhys Donoghue

0 Kudos
RhysDonoghue2
New Contributor III

Hmm, should have worked for 4.14 but wouldn't surprise me if there was a bug.  4.15 fixed a few bugs I was having with my app.

0 Kudos
NilsBabel1
Occasional Contributor

Hello, I'm getting the same CORS error.  I've added the trusted server with no effect.  Can you provide an example of the format?  Do you have to provide the port? I'm using 4.16.  I used to use esriConfig.defaults.io.corsEnabledServers.push on the 3.x api and it seemed like it actually worked.  But trustedServers.push doesn't seem to ever work.  Does it do the same thing?