Print templates with esri.request issue

2608
16
Jump to solution
10-31-2013 08:13 AM
SaqibAmin1
New Contributor
When I use the ESRI sample Print templates with esri.request (https://developers.arcgis.com/en/javascript/jssamples/widget_print_esri_request.html), I copy the code into a local html file, the "print" button doesn't display, however when I put an alert box in line 79, this somehow causes it to work in FireFox. To make debugging easier, I have replaced the original console.log() method to alert().

Any ideas?


<!doctype html> <html>   <head>     <meta charset="utf-8">     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">     <title></title>     <link rel="stylesheet" href="http://js.arcgis.com/3.7/js/dojo/dijit/themes/tundra/tundra.css">     <link rel="stylesheet" href="http://js.arcgis.com/3.7/js/esri/css/esri.css">     <style>       html, body { height: 100%; width: 100%; margin: 0; padding: 0; }       h3 { margin: 0 0 5px 0; border-bottom: 1px solid #444; padding: 0 0 5px 0; text-align: center; }       .shadow {         -moz-box-shadow: 0 0 5px #888;         -webkit-box-shadow: 0 0 5px #888;         box-shadow: 0 0 5px #888;       }       #map{ margin: 0; padding: 0; }       #feedback {         background: #fff;         color: #000;         position: absolute;         font-family: arial;         height: auto;         right: 20px;         margin: 5px;         padding: 10px;         top: 20px;         width: 300px;         z-index: 40;       }       #feedback a {         border-bottom: 1px solid #888;         color: #444;         text-decoration: none;       }       #feedback a:hover, #feedback a:active, #feedback a:visited {         border: none;         color: #444;         text-decoration: none;       }       #note { font-size: 80%; font-weight: 700; padding: 0 0 10px 0; }       #info { padding: 10px 0 0 0; }     </style>      <script src="http://js.arcgis.com/3.7/"></script>     <script>       var app = {};       require([         "esri/map", "esri/layers/FeatureLayer",          "esri/dijit/Print", "esri/tasks/PrintTemplate",          "esri/request", "esri/config",         "dojo/_base/array", "dojo/dom", "dojo/parser",          "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"       ], function(         Map, FeatureLayer,          Print, PrintTemplate,          esriRequest, esriConfig,         arrayUtils, dom, parser       ) {         parser.parse();          app.printUrl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task";          esriConfig.defaults.io.proxyUrl = "/proxy";                  app.map = new esri.Map("map", {           basemap: "hybrid",           center: [-117.447, 33.906],           zoom: 17,           slider: false         });          // add graphics for pools with permits         var permitUrl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/PoolPermits/MapServer/1";         var poolFeatureLayer = new FeatureLayer(permitUrl, {           "mode": FeatureLayer.MODE_SNAPSHOT         });   alert("Test"); // This is Line 79 ! This will work in FireFox, after commenting out, it will break         app.map.addLayer(poolFeatureLayer);          // get print templates from the export web map task         var printInfo = esriRequest({           "url": app.printUrl,           "content": { "f": "json" }         });         printInfo.then(handlePrintInfo, handleError);          function handlePrintInfo(resp) {           var layoutTemplate, templateNames, mapOnlyIndex, templates;            layoutTemplate = arrayUtils.filter(resp.parameters, function(param, idx) {             return param.name === "Layout_Template";           });                      if ( layoutTemplate.length == 0 ) {             alert("print service parameters name for templates must be \"Layout_Template\"");             return;           }           templateNames = layoutTemplate[0].choiceList;            // remove the MAP_ONLY template then add it to the end of the list of templates            mapOnlyIndex = arrayUtils.indexOf(templateNames, "MAP_ONLY");           if ( mapOnlyIndex > -1 ) {             var mapOnly = templateNames.splice(mapOnlyIndex, mapOnlyIndex + 1)[0];             templateNames.push(mapOnly);           }                      // create a print template for each choice           templates = arrayUtils.map(templateNames, function(ch) {             var plate = new PrintTemplate();             plate.layout = plate.label = ch;             plate.format = "PDF";             plate.layoutOptions = {                "authorText": "Made by:  Esri's JS API Team",               "copyrightText": "<copyright info here>",               "legendLayers": [],                "titleText": "Pool Permits",                "scalebarUnit": "Miles"              };             return plate;           });            // create the print dijit           app.printer = new Print({             "map": app.map,             "templates": templates,             url: app.printUrl           }, dom.byId("print_button"));           app.printer.startup();         }          function handleError(err) {           alert("Something broke: ", err);         }       });     </script>   </head>    <body class="tundra">     <div data-dojo-type="dijit/layout/BorderContainer"          data-dojo-props="design:'headline',gutters:false"          style="width: 100%; height: 100%; margin: 0;">       <div id="map"            data-dojo-type="dijit/layout/ContentPane"            data-dojo-props="region:'center'">          <div id="feedback" class="shadow">           <h3>             Print Templates Created from Info Returned by the Print Service using              <a href="https://developers.arcgis.com/en/javascript/jsapi/namespace_esri.html#request">esri.request</a>           </h3>           <div id="info">             <div id="note">               Note:  This sample uses an ArcGIS Server version 10.1 export web map task.             </div>                          <!-- that will be used for the print dijit -->             <div id="print_button"></div>              <div id="info">               <a href="https://developers.arcgis.com/en/javascript/jsapi/printtemplate.html">Print templates</a> are generated                from the Export Web Map Task's <a href="http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task">Layout_Template parameter</a>. This info is retrieved from the service                using <a href="https://developers.arcgis.com/en/javascript/jsapi/namespace_esri.html#request">esri.request</a>.              </div>            </div>         </div>       </div>     </div>   </body> </html>
0 Kudos
1 Solution

Accepted Solutions
JohnGravois
Frequent Contributor
glad to hear it Saqib.  please consider marking this thread as 'answered'.

View solution in original post

0 Kudos
16 Replies
NickO_Day
New Contributor III
Hi,
Try replacing the URL on line 63 that looks like this:
app.printUrl = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task";


With the URL for the same export web map task service from your local server... like this:
app.printUrl = "http://<YOURSERVER>/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task";
0 Kudos
SaqibAmin1
New Contributor
Hi Nick,

Many thanks for replying.

I have in my own personal project replaced all the URL's for my local ArcGIS server, including the print task, exactly the same thing occurs, when I put in an alert box the Print appears and it breaks when I comment out the alert box.

This only works in FireFox, it doesn't work on any other browser (I've tried Chrome, IE and Safari).

I was thinking if I could get the above code to work, I can apply these principles to my local project.

Thanks,
Saqib.
0 Kudos
JohnGravois
Frequent Contributor
for me this problem is reproducible by ignoring the alert, and simply commenting out the line that designates the proxy. 

i'm not sure why firefox is able to make the request to sampleserver6 without one, but the problem was resolved for me in Chrome after making sure that the proxy i was referencing was pointing at a valid location, and that it was actually functioning.

try snooping the web traffic in chrome for this specific request and make sure it succeeds.
http://localhost/proxy/proxy.ashx?http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utiliti...
0 Kudos
SaqibAmin1
New Contributor
John,

When I go to http://localhost/proxy/proxy.ashx?http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utiliti... and look through the Network tab in Chrome, I receive three Get methods.
I get a success on the (first) text/html file, on the second (default.css) I get a 304 Not modified and lastly a 204 No Content. I have attached a screenshot to make this point more clear.

Is this what you're expecting?
0 Kudos
JohnGravois
Frequent Contributor
hi sadiq, thanks for that screenshot.  i could have been clearer in my suggestion.  try snooping the web traffic from your original application looking specifically for the request to the print service.  this might help you better determine where things are breaking down.
0 Kudos
SaqibAmin1
New Contributor
Hi John,

I checked the Network stats in my local project and similar to your link my proxy link was not going through, as a test I changed all my URL's to point internally at the local server, hence bypassing the proxy and it worked.
The next step is I need to find out how to check my proxy is working, is there an easy way to tell the proxy page works, or if it is even defined?

Thanks,
Saqib.
0 Kudos
JohnGravois
Frequent Contributor
yup, just try the url i linked above directly in the browser.

if it 404s, the url is incorrect
if it 403s, the proxy.config is telling you that it doesnt think the url you are asking it to forward is in its own list.
if it 500s, the proxy is trying to retrieve the resource and encountering something unexpected.
0 Kudos
DonCaviness
New Contributor III
I am running into a similar issue trying make a call to my print service through the proxy page.  I am getting the 500 error when I watch the traffic via fiddler.  What could be causing this issue?
0 Kudos
JohnGravois
Frequent Contributor
@don
it could be a variety of things. 

are you able to use your proxy to display the REST services directory homepage?
are you able to login directly to the server hosting your proxy and load the print service url in the browser?
0 Kudos