1

I have OpenLayers ProxyHost configured

OpenLayers.ProxyHost= "form/proxy?url=";

, the url config of Mapfish Print Provider is configured as http://path/to/mapfish/print, and the doc tells that I can do that as it is accessible throug the proxy

This property requires that the print service is at the same origin as the application (or accessible via proxy).

, but the lib still tries to go to other origin directly and fails.

I cannot find where to configure GeoExt or ExtJS to go there through proxy.

The Mapfish Print doc tells to set some parameter of a mapfish variable, I did that but obviously with no success.

Please, help!

(Actually, it's GeoExt 2 for ExtJS 4, where the PrintProvider seems being renamed as GeoExt.data.MapfishPrintProvider)

fedd
  • 163
  • 6

3 Answers3

2

Adding the OpenLayers.ProxyHost will only proxy requests that OpenLayers itself makes and won't proxy requests from the GeoExt framework. But actually you do not need to use a proxy as you can put the request for the Print Capabilities into your page's header as a script tag like this:

<script src="http://path/to/mapfish/print/info.json?var=printCapabilities type="text/javascript"></script>

Then all you need to do in your print provider is reference the printCapabilities variable like this:

// The printProvider that connects us to the print service
var printProvider = new GeoExt.data.PrintProvider({
    method: "GET", // "POST" recommended for production use
    capabilities: printCapabilities, // from the info.json script in the html
    customParams: {
        mapTitle: "Printing Demo",
        comment: "This is a simple map printed from GeoExt."
    }
});

Then all requests for printing should work as the url is stored in the printCapabilities object. This is the approach I use for printing from my web appications.

CHenderson
  • 2,583
  • 15
  • 20
  • thanks! unfortunately the problem remains as after that it still needs to go to http://xxxxx/print/pdf/create.json with XHR, and it's against the same origin policy again. this url is configured in printCapabilities and is not available for me to edit. – fedd Jul 27 '12 at 10:05
  • 1
    I think in the PrintProvider you can swap the "capabilities" property for a "url" property instead. In which case you could do "form/proxy?url=http://path/to/mapfish/print" - then I think the PrintProvider will use this url which should route it through your proxy for all requests. – CHenderson Jul 27 '12 at 23:29
  • must be good idea! unfortunately i couldn't make it work as createURL and printURL of print capabilites were specified as absolute URLs, with the domain - I faced the same error. maybe it's an error of mapfish printing servlet deployment, need to talk to it's admins which takes time... – fedd Jul 30 '12 at 09:08
1

Because Geoext2 they remove override-ext-ajax.js, but you can mortify urls like this

printCapabilities.createURL="./ProxyHost.ashx?url="+printCapabilities.createURL;

printCapabilities.printURL="./FTProxyHost.ashx?url="+printCapabilities.printURL; 

printCapabilities is from http://localhost:8080/geoserver/pdf/info.json?var=printCapabilities.

It works fine for me.

Muke
  • 166
  • 4
0

The solution can be found here:

WMS server with CORS enabled?

I had the same problem and have been wrestling with it for 15 months. Set up your GeoExt print provider using the POST option instead of the GET option;

// The printProvider that connects us to the print service
var printProvider = new GeoExt.data.PrintProvider({
    method: "POST", // Use "POST" so that it can handle long print requests
    capabilities: printCapabilities, // from the info.json script in the html
    customParams: {
        mapTitle: "Printing Demo",
        comment: "This is a simple map printed from GeoExt."
    }
});

Using POST will cause a problem with the cross origin policy. i.e you will be requesting to http://xx.xx.xx.xx:8080/ from http://xx.xx.xx.xx and the server will refuse to answer the request.

To fix the cross origin problem paste this code snippet at the beginning of your javascript;

(function() {
    var cors_api_host = 'cors-anywhere.herokuapp.com';
    var cors_api_url = 'https://' + cors_api_host + '/';
    var slice = [].slice;
    var origin = window.location.protocol + '//' + window.location.host;
    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        var args = slice.call(arguments);
        var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]);
        if (targetOrigin && targetOrigin[0].toLowerCase() !== origin &&
            targetOrigin[1] !== cors_api_host) {
            args[1] = cors_api_url + args[1];
        }
        return open.apply(this, args);
    };
})();

Credit for this solution goes to: https://gis.stackexchange.com/users/638/thomasg77

Todd Krueger
  • 776
  • 11
  • 27