Executing the query with QueryTask
Once you've defined the nature of the query in a Query
object you can then use QueryTask
to execute the query.
First, create an instance of the QueryTask
object. You create a QueryTask
object by passing the URL of the layer against which the query will be executed to its constructor. Note from the code example that follows that the map service URL includes an index number which references a specific layer in the map service which will be queried:
myQueryTask = new
QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_CENSUS_USA/MapServer/5");
Once you have created the QueryTask
object you can perform a query against its layer passing a Query
object to its QueryTask.execute()
method.
QueryTask.execute()
accepts three parameters: the Query
object, a function to call when the operation succeeds, and a function to call if the operation fails.
The syntax for QueryTask.execute()
is provided as follows:
QueryTask.execute(objQuery...