Print dijit, CORS error

3758
13
08-20-2015 08:59 AM
AshleyPeters
Occasional Contributor III

I am having a very similar issue as is mentioned here: https://community.esri.com/message/399585. I've added in Print dijit and PrintTemplate task and get a print button. However, when I click it, it changes from Print to Printing, runs for a bit, then changes back to Print. When I check the Firefox developer tools, I get this error:

I've added in a proxy on my server and gone through the steps to enable CORS mentioned at enable cross-origin resource sharing. I'm still getting the error.

At this point, I'm not sure if it is an issue in my code, or that I need to place the proxy on my local machine, where I'm building. I can't turn on IIS on my local machine, as our IT folks don't allow it.

Here's the code I added in for printing and the proxy:

esriConfig.defaults.io.proxyUrl = "http://myURL/DotNet/proxy.ashx",
esriConfig.defaults.io.alwaysUseProxy = false;

//Create Printer Layouts Array
var myLayouts = [{
"name": "Print_Landscape",
"label": "Letter (Landscape Orientation)",
"format": "pdf"
}, {
"name": "Print_Portrait",
"label": "Letter (Portrait Orientation)",
"format": "pdf"
}];

//Create Print Templates
var myTemplates = [];
dojo.forEach(myLayouts, function (lo) {
var printTemp = new PrintTemplate();
  printTemp.layout = lo.name;
  printTemp.label = lo.label;
  printTemp.format = lo.format
  myTemplates.push(printTemp);
});

And this portion after creation of my map:

//Add Print Widget

var PrintWidget = new Print({

map: mapMain,

url: "https://myURL/rest/services/CustomPrint/GPServer/Export%20Web%20Map",

templates: myTemplates

}, "PrintWidg");

PrintWidget.startup();

Any help/guidance would be much appreciated! Thank you in advance!

Ashley

0 Kudos
13 Replies
DavidElies
New Contributor III

It looks like you've turned off alwaysUseProxy, (a good idea in my book), but that means you have to specify a proxy rule for your print server, something like this:

urlUtils.addProxyRule({

     "proxyUrl": "http://myURL/DotNet/proxy.ashx​",

     "urlPrefix": "https://myURL/rest/services/CustomPrint"

});

0 Kudos
AshleyPeters
Occasional Contributor III

David,

Thanks for the response. That seemed to clear up the CORS error, but now I'm getting another one:

I'll see what I can find on it.

Ashley

0 Kudos
DavidElies
New Contributor III

Unfortunately, the .NET proxy provided by ESRI turns every HTTP error into a 500 InternalServerError.  Some things to check:

Is the URI for the print service reachable? Any typos in the path? Have you tested the print service before? Is there a way to test it without the proxy? Can you debug the proxy.ashx?  I usually put a breakpoint in where to 500 error is generated (I forget where it is, I just search for '500' in the file).  Then I can see what the actual error is.  If you're able to and comfortable changing it, you can also, just for debugging, change the proxy to pass on the original error code/text/etc, rather than creating the 500 error.

0 Kudos
AshleyPeters
Occasional Contributor III

With that response, I wish I had been working with JavaScript longer than 3 weeks!

This is the first time I've created a print service, and the first time I've dealt with a proxy. So I'll have to do some research to be able to answer most of your questions. The print service is reachable and there are no typos. I have not tested the print service, though I'll see what I can do to accomplish that. And then I'll start going through the other things you've mentioned.

0 Kudos
DavidElies
New Contributor III

You might be able to test your print service by going back to your original error message (the XMLHttpRequest error), and copying the full URL in that message into the Firefox address bar.  If it you get an HTTP error, you know what to troubleshoot (the print service). If the print server gives you a message, you probably need to configure your print widget.  If you get a print image or document, it's probably a proxy config error.

0 Kudos
AshleyPeters
Occasional Contributor III

So at this point, I've done a few different things. I took a working Flex app and changed the print widget in it to hit the service I created. It pulled the layout templates in correctly, so I moved forward with trying to print. It gave a 400 error. I then checked my logs on server and it told me that contact could not be made with my map server. I then did some searching on that and one of the suggestions was to be sure I had the newest version of the proxy. I downloaded it again and added it fresh to my server. I have not made any changes to it yet. I tried to ping the proxy and I'm getting a 403 error and it says I must log in to see the site.

I believe at this point my issue is with my proxy and possibly the settings in IIS. In IIS, anonymous authentication is enabled and everything else is disabled.

0 Kudos
DavidElies
New Contributor III

You might need to check your proxy config to make sure it's allowing requests from your app and to your print service.  In the proxy.config file, make sure the allowedReferrers attribute includes your javascript application.  By default, it uses *, meaning all apps from any location (including yours) can use the proxy. Next you want to make sure that if mustMatch is set to true, that you add your print server url into the list of serverUrls.  Otherwise it'll block it.  Actually, now that I think about it, that must've been your problem earlier, but I forgot about that, since I keep it disabled for testing -- in our dev environment only .

0 Kudos
AshleyPeters
Occasional Contributor III

I've been able to ping the new proxy, but still getting a 500 error when I try to print. After spending some time with tech support, we still haven't been able to resolve the problem. They feel its likely a setting on the server.

0 Kudos
DavidElies
New Contributor III

Did you ensure that your proxy.config includes a serverUrls entry for your print server?

0 Kudos