0

I'm trying to generate a pdf file to print a map using GeoExt but have run into a problem. On Chrome I get the error message:

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

I think the error message tells me that its to do with the proxy so I have tried adding the following to the Config.yaml but not luck:

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

This should have allowed all local ip addresses and all hosts to access the create.json in Howard:8080 but for some reason I still get the error message.

However, on Firefox I get a slighly different behaviour. I dont get any error message but I end up triggering a printexception with an error message "Communication failure.

I'm stuck and out of ideas, can anyone help?

Here is the code:

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]
});

edit:

I have added the following code in 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(); 

But I still get the error message. Do I add Access-Control-Allow-Origin header at the Howard server? If so how do I do that?

Edit 2:

I have added the following in code behind:

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

I have also placed the following:

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

Both code blocks added on the machine that will call the pdf file in my web.config file and still no luck.

Edit 3.

The pdf file seems to generate correctly if I don't include any vector layers. That works for Firefox and chrome. I have separate issues with IE so can't test it there.

  • You need to set up an `Access-Control-Allow-Origin` header. – SLaks Apr 18 '12 at 16:10
  • How do you setup an Access-Control-Allow-Origin header? – user1341833 Apr 23 '12 at 08:27
  • I can retrieve features from Howard server by using a proxy.ashx. But creating a pdf map print from Howard server I need to define an Access-Control-Allow-Origin? I have tried to allow any hosts in config.yaml file but no success. – user1341833 Apr 23 '12 at 13:17
  • You need to configure the HTTP headers. Allwoing hosts just tells it what `Host:` requests to respond to. – SLaks Apr 23 '12 at 13:24
  • The response header contains Access-Control-Allow-Origin: * meaning it should let all hosts to access the resource PDF print file. I'm still stuck on where to configure the http headers. I think you mean Response.AddHeader() ? If so I have added it to my code behind and still no luck. – user1341833 Apr 23 '12 at 14:44
  • Can you post an example on how to configure HTTP header? – user1341833 Apr 24 '12 at 08:02

1 Answers1

0

I am using proxy method on server within current origin and on client side I am just rewriting print and create urls:

"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);
        }
                },

On server side something like that on 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);