1

I use Geoserver 2.8.1, Openlayers 2.12, GeoExt 1.1 on a Windows machine. I can successfully print a map using the Print Provider below:

var printProvider = new GeoExt.data.PrintProvider({
        method: "GET", // "POST" recommended for production use
        capabilities: printCapabilities,
        customParams: {
            mapTitle: mapTitle,
            comment: mapNote
        }
    });

In the previous example the "printCapabilities" variable points to this location and everything works fine;

http://99.99.99.99:8080/geoserver/pdf/info.json?var=printCapabilities

I have large complicated maps with many layers and using the "GET" option my print map requests frequently exceed the maximum request length and return an error.

I know I can change "GET" to "POST" in order to send longer map print requests. If I change my Print Provider to the following;

   var printProvider = new GeoExt.data.PrintProvider({
        url: "/geoserver/pdf",
        autoLoad: true,
        method: 'POST',
        customParams: {
            mapTitle: mapTitle,
            comment: mapNote
        }
    });

my print fails and FireBug gives me the following error explanation;

"XMLHttpRequest cannot load http://99.99.99.99:8080/geoserver/pdf/info.json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://99.99.99.99' is therefore not allowed access."

I already have an OpenLayers proxy setup as follows:

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

and I use the "URL-Rewrite" tool in Microsofts Internet Information Services (IIS) manager to redirect any URL's and avoid the same origin problem. It works for everything else but for some reason the POST option makes the server think that I am violating the same origin policy.

What do I have to do to make my system recognize that the print request is coming from the same server as everything else?

0

The solution to geoserver/mapfish printing using the POST option can be found here;

WMS server with CORS enabled?

Simply paste this code 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);
    };
})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.