Sample overview |
This example illustrates how to access the REST service by constructing an HTTP request and send it to SuperMap iServer; realizes certain map operations using REST APIs to demonstrate SuperMap iServer REST functionality.
For details on how to construct an HTTP request, please refer to Constructing REST Requests.
For sample code, please refer to %SuperMap iServer_HOME%\samples\code.
In this example, two JavaScript files are needed: json_parse.js and toJSON.js. json_parse.js provides the json_parse function for pasing JSON strings. toJSON.js provides the getcommit function for getting the XMLHttpRequest object and the toJSON function for converting the JavaScript object to JSON string. You can get the two JavaScript files in %SuperMap iServer_HOME%\samples\code\UseRESTAPI\HelloREST.
Firstly, construct an HTML page for result display.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>REST Sample</TITLE>
<script type="text/javascript" src="json_parse.js"></script>
<script type="text/javascript" src="toJSON.js"></script>
</HEAD>
<BODY>
<input type="button" name="Submit" value="Test" onclick="" /></input>
<br><br>
<div align=left id="container" style="left:50px;top:50px">
</BODY>
<script type="text/javascript">
//Add sample code
</script>
</HTML>
Description: Test is a button used to implement the sample code and container is a Div used to display the result. Replace "//Add sample code" with the sample code, here the sample code is a method. Then type the method name in the pair of quotation marks after “onclick=”. When you click Test, the result will be displayed in the container.
Construct an application to implement the GET request for the maps. Replace "//Add sample code" with the getMapsAll method below.
//Add sample code
function getMapsAll()
{
//Get the XMLHttpRequest object
var commit=getcommit();
//HTTP request method, here is GET
var method="GET";
//The request URI, that is, the maps resource
var uri="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
commit.open(method,encodeURI(uri),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
//alert(commit.responseText);
//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');
//Show all responses
container.innerHTML +=toJSON(response);
}
Then assign the action for the click event of Test.
<input type="button" name="Submit" value="Test" onclick="getMapsAll()" /></input>
Start SuperMap iServer REST services, and click Test on the HTML page to get the response for implementing the GET request for http://localhost:8090/iserver/services/map-china400/rest/maps.json.
Till now, a simple HTTP request is finished. As to SuperMap iServer REST services, each resource has a URI corresponding to it and implementing different HTTP methods on the URI is identical to implementing different operations on the resource. Parameters of the HTTP request is contained in the URI or entity body.
Tips for sample code:
The returned content (commit.responseText) is parsed into a JavaScript object for the convenience of extracting content of our interest by referencing the structure of the server response.
Asynchronous communication is unabled (var async=false;) to simplify the code. Therefore, the script after the send() method will be implemented after the server response. In practical applications, asynchronous communication is always enabled and the server response is processed when the onreadystatechange event of XMLHttpRequest is triggered.
The following samples realize corresponding functions. A complete sample can be constructed as above.