我知道之前有人问过这个问题,但我对实现有疑问,所以我把它贴在这里。我在 mapfish 插件的帮助下打印使用 Geoserver 创建的地图,并且在我正在制作的 ajax 请求中出现解析错误。这是负责打印地图和错误的代码片段:
var spec = {
"mapTitle":"MyMap",//"DistrictMap",
"units":"degrees",
"srs":"EPSG:4326",
"layout":"A4 portrait",//"A4 Landscape",
"dpi":300,//300,
"layers": [
{
"type": 'WMS',
"layers": layers_param,
"baseURL": 'http://localhost:8081/geoserver/wms',
"format": 'image/jpeg'
}
],
"pages": [
{
"center": [6, 45.5],
"scale": 4000000,
"dpi": 150,
"geodetic": false,
"rotation":0,
"mapTitle":"A custom title",
"comment":"A custom comment",
"mapComments":"kkomment"
}
]
};
var spec_str = JSON.stringify(spec);
$.ajax({
type: "POST",
url: 'http://localhost:8081/geoserver/pdf/create.json',
dataType:'json',
data:spec_str,
success : function(){
alert('Hello');
}
});
它在控制台中引发以下错误:
Error while generating PDF:
java.lang.RuntimeException: Cannot parse the spec file: A JSONObject text
must begin with '{' at character 0:
at org.mapfish.print.MapPrinter.parseSpec(MapPrinter.java:190)
at.........and some more lines of crap!
更新:当我在请求中添加“contentType”:“application/json”时..我收到另一个错误说明:INVALID CORS REQUEST