在用ArcGIS Server JavaScript開發中,經常遇到和其它平台對接或者調用SOE時,需要用js訪問rest頁面獲得頁面值,下面示例代碼:
(注:此處是一個非常簡單的示例,根據arcgis server頁面的query操作,設置where="1=1"后構造的url,然后獲取要素的示例)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> <title>JSON Content</title> <script>var dojoConfig = { isDebug: true };</script> <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/"></script> <script> var url = "http://localhost:6080/arcgis/rest/services/testGetData/MapServer/0/query?where=1%3D1&text=&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&f=json"; var xmlHttp=GetXmlHttpObject(); function getContent() { xmlHttp.open("GET",url,true); //submit the URL request xmlHttp.send(null); xmlHttp.onreadystatechange= function() { // Here we process the response var responseText=xmlHttp.responseText; alert(responseText); } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } return xmlHttp; } </script> </head> <body style="font-family: Arial Unicode MS,Arial,sans-serif;"> <p> Enter URL here: <input type="text" disabled="true" id="url" size="75" /> <input type="button" value="GO" onclick="getContent();" /> <span id="status"></span> </p> <p> </body> </html>
運行后執行,彈出結果窗體(以json格式返回前台)