GeoExt PDF打印:“错误消息:Access-Control-Allow-Origin不允许原点http:// localhost:55608”

时间:2012-04-18 16:07:57

标签: printing openlayers geoserver print-module

我正在尝试生成一个pdf文件,使用GeoExt打印地图,但遇到了问题。在Chrome上,我收到错误消息:

XMLHttpRequest cannot load http://Howard:8080/geoserver/pdf/create.json. Origin http://localhost:55608 is not allowed by Access-Control-Allow-Origin.

我认为错误消息告诉我它与代理有关,所以我尝试将以下内容添加到Config.yaml但不是运气:

- !localMatch
  dummy: true
- !ipMatch
  host: 0.0.0.0
  mask: 0.0.0.0

这应该允许所有本地IP地址和所有主机访问Howard:8080中的create.json但由于某种原因我仍然收到错误消息。

但是,在Firefox上我的行为略有不同。我没有收到任何错误消息,但我最终触发了一个错误消息“通信失败。”

的printexception。

我被困在想法中,有人可以帮忙吗?

以下是代码:

var printProvider =  new GeoExt.data.PrintProvider({
              //method: "GET", // "POST" recommended for production use
              method: "POST", // "POST" recommended for production use

              //capabilities: g_printCapabilities,  // provide url instead for lazy loading

              url: OpenLayers.ProxyHost + "http://Howard:8080/geoserver/pdf/",
              //url: "http://Howard:8080/geoserver/pdf/",
              autoLoad: true,

              customParams: {
                mapTitle: "GeoExt Printing Demo",
                comment: "This demo shows how to use GeoExt.PrintMapPanel with OSM"
              }
          });


var printPage = new GeoExt.data.PrintPage({
    printProvider: printProvider
});

var mapPanel = new GeoExt.MapPanel({
    title: "Map",
    region: "center",
    height: 400,
    width: 600,
    map: map,

    bbar: ["->", {
        text: "Print",
        handler: function() {
            // convenient way to fit the print page to the visible map area
            printPage.fit(mapPanel, true);

            printProvider.print(mapPanel, printPage);
        }
    }]
});

// create a panel and add the map panel and grid panel
// inside it
var mainPanel = new Ext.Panel({
    renderTo: "mainpanel",
    layout: "border",
    height: 800,
    width: 600,
    items: [mapPanel]
});

编辑:

我在javascript中添加了以下代码:

var invocation = new XMLHttpRequest();  
var url = 'http://Howard:8080/geoserver/pdf/create.json';  

function callOtherDomain() {  
  if(invocation) {      
      invocation.open('POST', url, true);  
      invocation.onreadystatechange = function (){
          alert("invocation.onreadystatechange");
      }  
      invocation.send();   
  }  
}  
callOtherDomain(); 

但我仍然收到错误消息。我是否在Howard服务器上添加了Access-Control-Allow-Origin标头?如果是这样,我该怎么做?

编辑2:

我在后面的代码中添加了以下内容:

Response.AddHeader("Access-Control-Allow-Origin", "*");
Response.AddHeader("Access-Control-Allow-Headers", "POST");
Response.AddHeader("Access-Control-Allow-Headers", "GET");

我还提出了以下内容:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>

在我的web.config文件中调用pdf文件的机器上添加了两个代码块,但仍然没有运气。

编辑3。

如果我不包含任何矢量图层,则pdf文件似乎正确生成。这适用于Firefox和Chrome。我有IE的单独问题所以无法在那里测试。

1 个答案:

答案 0 :(得分:0)

我在当前来源的服务器上使用代理方法,在客户端我只是重写打印并创建网址:

"loadcapabilities": function () {
                    if (self.printProvider.capabilities.createURL.indexOf('url') === -1) {
            self.printProvider.capabilities.createURL = GMARC.geoserverPrintProxy + '?url=' + encodeURIComponent(self.printProvider.capabilities.createURL);
            self.printProvider.capabilities.printURL = GMARC.geoserverPrintProxy + '?url=' + encodeURIComponent(self.printProvider.capabilities.printURL);
        }
                },

在服务器端,类似于node.js上的内容:

url = 'http://geoserveripaddress:8080/geoserver/pdf/' + self.c.req.params.id;
        var method = self.c.req.params.id === 'info.json' ? 'GET' : 'POST', params = {};
request(url,
            {
                auth: {
                    "user": username,
                    "password": password,
                    'sendImmediately': false
                },
                method: method,
                json: params
            }
        ).pipe(self.c.res);