Getting maps list |
The maps resource is the collection of the maps on the server. By performing the GET request on the maps resource, the map information will be returned.
To perform the GET request on the maps resource: http://localhost:8090/iserver/services/map-china400/rest/maps.json, in which localhost is the server name and .json indicates the response returned is in JSON format, construct the the getMapsList() method as shown below.
function getMapsList()
{
//Get the XMLHttpRequest object
var commit=getcommit();
//HTTP request method, here is GET
var method="GET";
//The request URI
var url="http://localhost:8090/iserver/services/map-china400/rest/maps.json";
//Asynchronous communication setting
var async=false;
//Specify the user name and password when the server requires authentication
var user="";
var password="";
//Entity Body
var entry=null;
//Establish HTTP connection. Encode the URL to ensure the accuracy of the URI
commit.open(method,encodeURI(url),async,user,password);
//Set the Message Headers
commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//Send the HTTP request
commit.send(entry);
//commit.responseText is the received server response
}
To process the response returned from the server, the structure of the JSON string must be understood. Please refer to the page for maps resource in the REST API section.
According to the response structure for the maps resource, the name and path fields of the elements in the map collection can be got. Add the following code in the getMapsList() method.
//Parse the json string returned from the server to a JavaScript object
var response = json_parse(commit.responseText, null);
//Gets the Div container for display.
var container = document.getElementById('container');
//Get the number of the maps
var len=response.length;
container.innerHTML="";
for(var i=0;i<len;i++)
{
container.innerHTML += '<li>Map name: ' + response[i].name + ', URL: ' + response[i].path + '</li>';
}
The result is as below.