您的位置:首页 > Web前端 > JavaScript

javascript调用rest地址,获取页面值

2013-05-24 15:17 381 查看
在用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格式返回前台)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: