I do understand how the proxy helps in transferring the XMLHttpRequest
between the ArcServer and the Server (IIS/Apache) but where I am confused is that the first time the Html Doc loads, doesn't the Server need to request the ArcServer then as well. How does that request complete without the proxy
?
I mean the first time the page loads it does not need that proxy
and still the map is loaded in the Map container. But later if we send a XMLHttpRequest
to the Geoprocessing Tool, the response from the ArcServer can only be received by the Server if we have setup the proxy, right?
So how does it happen the first time and not the second time?
-
resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/…Mapperz– Mapperz ♦2012年09月10日 19:00:17 +00:00Commented Sep 10, 2012 at 19:00
-
1@Mapperz while that info still applies, that link is out of date, current version is here: help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp/…Derek Swingley– Derek Swingley2012年09月10日 22:18:42 +00:00Commented Sep 10, 2012 at 22:18
1 Answer 1
The JS API makes extensive use of JSONP, and this is how service metadata is retrieved. Once the JS API knows about a map service, it can insert tags to display map tiles or images from dynamic map services.
You're correct that there are certain cases where a proxy is required. You can read up about them in the "Using a Proxy" section of the Inside esri.request help topic.
Edit: Providing more info below about how esri.Map, map services and geoprocessing services work.
Accessing map services from the ArcGIS Server REST API does not require a proxy or CORS. This is because map service info can be retrieved via JSONP, which circumvents browsers' same origin policy. Once a map service is created and added to a map, the map tiles (or map images if the service is not cached) are inserted into the page by creating img tags. You can see this if you look at a page's DOM via Firefox or Chrome's developer tools. Since img tags (as well as script and link tags) are not subject to same origin, this works without a proxy or CORS.
For GP services, things are a little different. GP services usually take a number of inputs, and the request (the URL specifying input parameters) to a GP service is usually quite long. Because certain browsers impose a limit on URL length, to ensure cross browser compatibility, the JS API will switch to doing a POST rather than a GET when a URL is longer than ~2k characters. Cross domain POSTs are prohibited by same origin, and this is why a proxy (or CORS) is required. If you are using a GP service that accepts a small number of inputs, and only accepts a point (lines and polygons quickly increase URL length because every vertex is specified part of the request), it is possible possible to use GP service without a proxy or CORS. The key thing to remember is that if a URL is less than ~2k characters, and the request can be completed with a GET request, a proxy/CORS are not required. If the request must be done via a POST (an edit operation, for instance), and the service you're using is not on the same domain as your application, a proxy or CORS is required.
-
Thanks @Derek, but my question still remains unanswered. I do know how to setup a proxy or setup CORS but what I am not able to understand, is How come, without setting up the proxy or CORS, the map from
sampleserver
loads in the map container as the document loads but when I am trying to accesses the geoprocessing tool from the samesampleserver
i need proxy?Sam007– Sam0072012年09月11日 16:50:55 +00:00Commented Sep 11, 2012 at 16:50 -
@Sam007 added more info, please see my answer above.Derek Swingley– Derek Swingley2012年09月11日 21:24:56 +00:00Commented Sep 11, 2012 at 21:24
-
Thanks a lot @Derek. I was finally able to understand. Really explained well in detail. The
img
tag was especially the bullseye. I have seen the map represented as tiles, but never occured to me that they are being transferred asimg
, which is common sense. Thanks that was a great explainationSam007– Sam0072012年09月11日 21:40:13 +00:00Commented Sep 11, 2012 at 21:40 -
@Sam007 you're welcome and my pleasure, glad I could help :)Derek Swingley– Derek Swingley2012年09月11日 22:16:25 +00:00Commented Sep 11, 2012 at 22:16
-
Derek, THANK YOU! For posting so many helpful comments here, and even more so for posting working examples on JSFiddle. Postings from both you and Kelly have been absolutely vital in my learning of Javascript and the ESRI API. Just wanted to say thank you.user16713– user167132013年04月01日 13:32:45 +00:00Commented Apr 1, 2013 at 13:32
Explore related questions
See similar questions with these tags.