Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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 Sep 10 '12 at 22:18
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
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 same sampleserver i need proxy? –  Sam007 Sep 11 '12 at 16:50
    
@Sam007 added more info, please see my answer above. –  Derek Swingley Sep 11 '12 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 as img, which is common sense. Thanks that was a great explaination –  Sam007 Sep 11 '12 at 21:40
    
@Sam007 you're welcome and my pleasure, glad I could help :) –  Derek Swingley Sep 11 '12 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 Apr 1 '13 at 13:32
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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