我正在尝试使用OpenLayers.Request.GET
以 JSON 格式从 url 加载数据。
这是请求(注意:url 工作正常,它以 JSON 格式提供数据):
var request = OpenLayers.Request.GET({
url: "http://localhost:8080/geoserver/wrspc/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=wrspc:layer1&maxFeatures=60&outputFormat=json",
callback: handler
});
对于处理程序,我尝试获取request.responseText
并在 json 文件中显示特定键,如下所示:
var obj;
function handler(request) {
obj = request.responseText;
alert (obj.features[0].indicator);
}
这是我的 JSON:
{"type":"FeatureCollection","features":[{"type":"Feature","id":"titid","geometry":{"type":"MultiPolygon","coordinates":[[[[3694.7863290442,3749.0463695516],[9328.2052648721,3756.61081112875],[3694.18117371807,3861.9059202327],[9340.68659347435,3834.4171230714],[9334.7863290442,3749.0463695516],[3634.7863290442,3839.0463695516]]]]},"geometry_name":"the_geom","properties":{"name1":"asme","number":9130,"indicator":"20","gid":939}}],"crs":{"type":"EPSG","properties":{"code":"2684"}}}
但我收到此错误:(注意 TestPrint.html:506 是警报行)
Uncaught TypeError: Cannot read property '0' of undefined TestPrint.html:506
GeoExt.form.FormPanel.listeners.actioncomplete TestPrint.html:506
h.Event.fire ext-all.js:21
h.Observable.fireEvent ext-all.js:21
(anonymous function) ext-all.js:21
h.Event.fire ext-all.js:21
h.Observable.fireEvent ext-all.js:21
Ext.form.BasicForm.Ext.extend.afterAction ext-all.js:21
GeoExt.form.SearchAction.Ext.extend.handleResponse SearchAction.js:147
OpenLayers.Protocol.WFS.v1.OpenLayers.Class.handleRead OpenLayers.js:843
(anonymous function) OpenLayers.js:413
(anonymous function) OpenLayers.js:62
OpenLayers.Request.runCallbacks OpenLayers.js:509
d.onreadystatechange OpenLayers.js:508
b.dispatchEvent OpenLayers.js:751
c OpenLayers.js:744
_object.onreadystatechange OpenLayers.js:748