Select to view content in your preferred language

Error on using IdentityManager when loading dojo with Esri Javascript Api 4.x

1213
6
Jump to solution
11-01-2023 03:40 AM
AlbertoPinilla
Emerging Contributor

I am migrating my web application from the 3.x  to 4.x Esri javascript api. I need to load "esri/identity/IdentityManager" module to access by token to my arcgis server rest services. It works fine, if I don't load dojo. But It fails when I load it. It comes out this error:
dojo.js.uncompressed.js:1770 GET https://ajax.googleapis.com/ajax/libs/dojo/1.14.1/esri/identity/IdentityManager.js net::ERR_ABORTED 404

It looks like the application is looking for the esri module in the dojo toolkit...

Below I am adding a sample in which I've commented the references to the IdentityManager . This way it works. For this sample there is no need to use the token, but I include it to make it easier to understand the case.

Thanks in advance.

 

 

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>DOJO and Esri javascript api 4.X</title>
	<!-- Load dojo  -->
	<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.14.1/dojo/dojo.js"></script>

    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.28/esri/themes/light/main.css"
    />
    <script src="https://js.arcgis.com/4.28/"></script>

    <script>
      require([
	  
	  "esri/layers/FeatureLayer",
        "esri/rest/support/Query",
		//If commented references to IdentityManager, there is no problem
		//"esri/identity/IdentityManager", 
		 "dojo/dom", "dojo/on", 
		"dojo/domReady!"
      ], function (FeatureLayer, Query, 
	 // esriId,
	  dom, on) {
	  	
		var tokenStr="63dJGeEz6333YPz6T07uLpGEbbVsnRPLqVVnEVkqhUq.";
		
			var token = {
			  "server": "https://sampleserver6.arcgisonline.com/arcgis/rest/services",				
			  "token": tokenStr
			};
		//	esriId.registerToken(token);
			
      const countiesLayer = new FeatureLayer({
          url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3"
        });
	
		let queryCounties = countiesLayer.createQuery();
		queryCounties.returnGeometry = false;
		queryCounties.where = "state_name ='Washington'";
		queryCounties.outFields = [ "OBJECTID", "state_name" ];
		
		on(dom.byId("execute"), "click", execute);

		
	   function execute () {
		 countiesLayer.queryFeatures(queryCounties)
		  .then(function(response){
		document.write(response.features[0].attributes.state_name);		
		});

      }	
			
      });
    </script>
  </head>
  <body>   
    <input id="execute" type="button" value="Query County">
  </body>
</html>

 

 

0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

It appears that adding the dojoConfig settings (particularly with the "packages" property set) prior to loading dojo resolves the problem:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>DOJO and Esri javascript api 4.X</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.28/esri/themes/light/main.css" />
<script type="text/javascript">
	window.dojoConfig = {
		async: true,
		cacheBust: "0.0.1",
		has: {},
		isDebug: true,
		locale: "en-us",
		parseOnLoad: false,
		packages: [
			{name:"esri", location:"https://js.arcgis.com/4.28/esri"}
		]
	};
</script>
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.14.1/dojo/dojo.js"></script>
<script src="https://js.arcgis.com/4.28/"></script>
<script>
	require([
		"esri/layers/FeatureLayer",
		"esri/rest/support/Query",
		"esri/identity/IdentityManager", 
		"dojo/dom",
		"dojo/on", 
		"dojo/domReady!"
	], function (FeatureLayer, Query, esriId, dom, on) {
		var tokenStr = "63dJGeEz6333YPz6T07uLpGEbbVsnRPLqVVnEVkqhUq.";
		var token = {
			"server": "https://sampleserver6.arcgisonline.com/arcgis/rest/services",				
			"token": tokenStr
		};

		esriId.registerToken(token);
			
		const countiesLayer = new FeatureLayer({
			url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3"
		});

		let queryCounties = countiesLayer.createQuery();
		queryCounties.returnGeometry = false;
		queryCounties.where = "state_name ='Washington'";
		queryCounties.outFields = [ "OBJECTID", "state_name" ];
		
		on(dom.byId("execute"), "click", execute);

		function execute () {
			countiesLayer.queryFeatures(queryCounties).then(function(response) {
				document.write(response.features[0].attributes.state_name);		
			});
		}	
	});
</script>
</head>
<body>   
	<input id="execute" type="button" value="Query County" />
</body>
</html>

View solution in original post

6 Replies
JoelBennett
MVP Regular Contributor

It appears that adding the dojoConfig settings (particularly with the "packages" property set) prior to loading dojo resolves the problem:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>DOJO and Esri javascript api 4.X</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.28/esri/themes/light/main.css" />
<script type="text/javascript">
	window.dojoConfig = {
		async: true,
		cacheBust: "0.0.1",
		has: {},
		isDebug: true,
		locale: "en-us",
		parseOnLoad: false,
		packages: [
			{name:"esri", location:"https://js.arcgis.com/4.28/esri"}
		]
	};
</script>
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.14.1/dojo/dojo.js"></script>
<script src="https://js.arcgis.com/4.28/"></script>
<script>
	require([
		"esri/layers/FeatureLayer",
		"esri/rest/support/Query",
		"esri/identity/IdentityManager", 
		"dojo/dom",
		"dojo/on", 
		"dojo/domReady!"
	], function (FeatureLayer, Query, esriId, dom, on) {
		var tokenStr = "63dJGeEz6333YPz6T07uLpGEbbVsnRPLqVVnEVkqhUq.";
		var token = {
			"server": "https://sampleserver6.arcgisonline.com/arcgis/rest/services",				
			"token": tokenStr
		};

		esriId.registerToken(token);
			
		const countiesLayer = new FeatureLayer({
			url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3"
		});

		let queryCounties = countiesLayer.createQuery();
		queryCounties.returnGeometry = false;
		queryCounties.where = "state_name ='Washington'";
		queryCounties.outFields = [ "OBJECTID", "state_name" ];
		
		on(dom.byId("execute"), "click", execute);

		function execute () {
			countiesLayer.queryFeatures(queryCounties).then(function(response) {
				document.write(response.features[0].attributes.state_name);		
			});
		}	
	});
</script>
</head>
<body>   
	<input id="execute" type="button" value="Query County" />
</body>
</html>
AlbertoPinilla
Emerging Contributor

Hi, Joel, I sensed that the solution was related to dojoConfig but I couldn't get any results. Thanks a lot for the answer!

0 Kudos
ReneRubalcava
Honored Contributor

If you reference dojo via cdn and the Maps SDK via CDN, you are loading the dojo loader twice.

You should probably point the dojo path to previous versions of the Maps SDK CDN. You can even reference all the old dojo related libs if you need them, but it is highly recommended you stop using these and migrate to native js/browser implementations.

https://developers.arcgis.com/javascript/latest/4.25/#removal-of-non-esri-packages-from-cdn

 

<script>
  window.dojoConfig = {
    packages: [{
        name: "dojo",
        location: "../../3.42/dojo"
      },
      {
        name: "dijit",
        location: "../../3.42/dijit"
      },
      {
        name: "dojox",
        location: "../../3.42/dojox"
      },
      {
        name: "dgrid1",
        location: "../../3.42/dgrid1"
      },
      {
        name: "dstore",
        location: "../../3.42/dstore"
      }
    ]
  };
</script>

 

AlbertoPinilla
Emerging Contributor

Thank you very much for your answer, Rene, I take into account. When you recommend migrate to native js/browser implementations, do you mean giving up using dojo, jquery,.. libraries?

0 Kudos
ReneRubalcava
Honored Contributor

dom.byId can be replaced with document.getElementById, and dojo/on is just a shortcut for addEventListener. You don't need domReady in modern browsers today. There's really no reason to rely on those dojo utilities in your apps today.

0 Kudos
AlbertoPinilla
Emerging Contributor

Thanks for your answer, Rene.

0 Kudos