Step 3 Adding JavaScript Code |
To get the map list from the server, you need to add your code to the tage of <script/>:
<script type="text/javascript">
//Add your code
</script>
Add the getcommit() method to get the XMLHTTP component supported by the browser.
//Add your code
//Get XMLHTTP components supported by browsers
function getcommit()
{
var commit = null;
try{
commit = new ActiveXObject("Msxml2.XMLHTTP");
}catch(ex){
try{
commit = new ActiveXObject("Microsoft.XMLHTTP");
}catch(ex){
this.commit=null;
}
}
if(!commit && typeof XMLHttpRequest != "undefined"){
commit = new XMLHttpRequest();
}
return commit;
}
Add the getMapsList() method below the getcommit() method to get the map list.
function getMapsList()
{
//Get an XMLHttpRequest object
var commit=getcommit();
//Method types for HTTP request. Here it is GET.
var method="GET";
//Request URL
var url="http://localhost:8090/iserver/services/map-china400/rest/maps.json";
//Set whether it is asynchronous communication or not
var async=false;
//Specify your user name and password
var user="";
var password="";
//Entity Body content
var entry=null;
//Build HTTP connection
commit.open(method,url,async,user,password);
//Set Message Headers
commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//Send HTTP Request
commit.send(entry);
//commit.responseText is the server response that you receive
//Parse a JSON string to an Object.
var response = json_parse(commit.responseText, null);
//Get a Div container to display the result
var container = document.getElementById('container');
//Get the number of 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>';
}
}