Select to view content in your preferred language

How to make ESRI Rest call from javascript

8503
19
02-22-2017 12:21 AM
roshnibasu
Occasional Contributor

How do I call the REST API https://www.arcgis.com/sharing/generateToken?username=<>&password=<>&referer=https://geoenrich.arcgis.com&f=json from a javascript method? I tried using jquery ajax request and found it working sometime and sometime the request is always pending.

And for the the following url, I do not find any specific query that I need to use? In Arc GIS 3.19 version I saw  esri.tasks.locationproviders.StandardGeographyQueryLocationProvider but in 4 it is not there. If I make normal ajax call using jquery it does not work at all.

Please help me to resolve the problem.

19 Replies
FC_Basson
MVP Regular Contributor

In the "then" part of the esri.request

console.log(resp); should be console.log(response.data);

or

your resp = response.data must be defined before the console.log(resp); output.

The return resp; might also return nothing, because the request has not completed yet.

roshnibasu
Occasional Contributor

Hi Basson,

That part of code is typo. What exactly iwant is the following two lines need to be executed:

var myResp = myTestFunction('myuname','mypass');
console.log(myResp);

0 Kudos
FC_Basson
MVP Regular Contributor

Do it like in Robert's example where the result is returned in the showResults function then instead of 

var myResp = myTestFunction('myuname','mypass');
console.log(myResp);

just do 

myTestFunction('myuname','mypass');

and change the request "then" function to 

.then(function(response){
   showResults(response);

}

roshnibasu
Occasional Contributor

i need to store the response and use it in some other method. Thats the reason I want to do it that way.

0 Kudos
FC_Basson
MVP Regular Contributor

No problem - make resp a global variable which you assign the value to in the showResults function.

0 Kudos
roshnibasu
Occasional Contributor

My requirement is to fetch the data. and use it somewhere else.

roshnibasu
Occasional Contributor

I should have the method to call using ESRI API in a separate class and then call that from other class.

0 Kudos
FC_Basson
MVP Regular Contributor

Roshni, I think your original question has been answered and solved.  You'll have to figure out a way to pass the variable data between functions.

roshnibasu
Occasional Contributor

Ok. Thanks so much for all the help.

If any of you come across to the kind of issue I'm facing please provide suggestion.

0 Kudos
RochelleWolfe
Occasional Contributor

This is a very helpful example for me too! It does pull in that data successfully, however I am also showing a CORS error in the console. Should that not be appearing with examples? I also have it after this tutorial Get an access token | ArcGIS for Developers and this tutorial Query a feature layer | ArcGIS for Developers .

0 Kudos