Select to view content in your preferred language

ArcGIS Javascript API - Proxy Page

8856
8
03-31-2015 11:39 AM
PeterHunt3
Deactivated User

I'm developing a Web Application that is using the ArcGIS Javascript API.  When adding a Feature Layer I'm using the setDefinitionExpression to limit the features that are returned.  The expression used can be quite large (greater than 2000 in length).

Because the expression can be large, I am attempting to use the Proxy page, however I am seeing unusual results.  I am finding that the proxy page is not always used, even though I am using the exact same query expression.  I have conformed this by utilizing IE's debugger tool and also using Fiddler.

In my application if I add 2 layers (Tiled and Dynamic) prior to adding the Feature Layer I consistently see that the Proxy page is not being used. I can see via the debugging tools that a POST is being performed directly to my REST service.  The content-length is over 6000.

If I add just the Tiled layer prior to adding the Feature Layer, I can see that an attempt is made to use the Proxy page.  The proxy page is having issues, therefore my Feature Layer does not get added.

Can someone please explain why the Proxy page is not always used even though it would seem that it should be, given the length of the query string used in the setDefinitionExpession.  I am very confused as to why layers added prior to adding a Feature Layer would affect the use of the proxy.

0 Kudos
8 Replies
ChrisSmith7
Honored Contributor

Just curious - what happens if you always enable the proxy?

esriConfig.defaults.io.alwaysUseProxy = true;

0 Kudos
PeterHunt3
Deactivated User

When I set alwaysUseProxy = true, an attempt is made to use the proxy, in the scenario where it was not previously used.  So setting alwaysUseProxy to true would seem to force it.

Interestingly the first layer I add is from server.arcgisonline.com.  I added the url in the Proxy config (see below), however it fails.  Below are the errors I see in the console.  I get the same errors when the Proxy is used to access my own service.

Proxy.config:

<serverUrls>

       <serverUrl url="http://server.arcgisonline.com/ArcGIS/rest/services"

                   matchAll="true"/>

</serverUrls>

Errors:

SEC7120: Origin http://localhost:3042 not found in Access-Control-Allow-Origin header.

SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

RequestError: Unable to load http://phunt-pc/EsriProxy/proxy.ashx?http://server.arcgisonline.com/ArcGIS/rest/services/World_Stree... status: 0

0 Kudos
KristianEkenes
Esri Regular Contributor

Peter Hunt​ Can you post code snippets/examples of how you're using the proxy inside your app?

0 Kudos
PeterHunt3
Deactivated User

I have very little code dealing with the proxy.  Before I create the map (using my own dojo class), I set the esriConfig.defaults.io.proxyUrl.  See below.  Normally the alwaysUseProxy is set to false.

Below I have also included the values from my esriConfig.defaults.io

The only reason I started using the proxy was that I started getting inconsistent failures and in the console there was an error complaining that the proxyUrl was null.  I assumed this was the result of an attempt e to use the proxy, due to the large query string I was using.  I therefore installed the proxy and added the settings below.  My issue with the null proxyUrl went away and so did my inconsistent failures.

Recently I changed the services I was using to get different data, and I ran into another issue.  While investigating I found that my proxy was in fact not being used even when there was a large query string (> 2000).  I could see a POST being performed directly to my service.  So I am now confused about when the proxy gets used.

require(["tps/map",

        "esri/config",

        "dojo/domReady!"],

        function (tpsMap, esriConfig) {

            // Setup Esri Config

            esriConfig.defaults.io.proxyUrl = "http://phunt-pc/EsriProxy/proxy.ashx"

            esriConfig.defaults.io.alwaysUseProxy = true;

            // Create and display Map

            tpsMap = new tpsMap(oMapConfiguration);

            tpsMap.mapQuery = oMapQuery;

            tpsMap.createMap("divMap");

        });

esriConfig.defaults.io

{...}

    [Methods]: {...}

    [prototype]: {...}

    alwaysUseProxy: true

    corsDetection: true

    corsEnabledServers: [...]

    corsStatus: {...}

    maxRequestWorkers: 5

    postLength: 2000

    proxyRules: []

    proxyUrl: "http://phunt-pc/EsriProxy/proxy.ashx"

    timeout: 60000

    useCors: true

    useWorkers: "on-request"

    webTierAuthServers: []

0 Kudos
KristianEkenes
Esri Regular Contributor

There are a couple of options you can try.

1. The error you mentioned above (SEC7120: Origin http://localhost:3042 not found in Access-Control-Allow-Origin header) looks like a CORS issue. When consuming services that aren't hosted on a CORS enabled server, you can push them to esriConfig.defaults.io.corsEnabledServers. http://server.arcgisonline.com/ArcGIS/rest/services​ should work though since "server.arcgisonline.com" already exists in that array. I typically use "services.arcgisonline.com" instead for my applications and it has always worked fine for me. So you could try swapping it out in your app and proxy config file. I would also exclude the "/ArcGIS/rest/services" from the url in the config file. Again, it shouldn't really matter either way, but it could make a difference.

2. You can also use urlUtils.addProxyRule() to set up proxy rules within the app. This is the method I prefer. You can replace the snippet above with:

            // Setup Esri Config

            esriConfig.defaults.io.proxyUrl = "http://phunt-pc/EsriProxy/proxy.ashx"

            esriConfig.defaults.io.alwaysUseProxy = true;

require(["esri/urlUtils", ... ], function(urlUtils, ...){

  ...
  ...

  urlUltils.addProxyRule({
    urlPrefix: "server.arcgisonline.com",    //whatever you have in your config file here
    proxyUrl: "http://phunt-pc/EsriProxy/proxy.ashx"
  });

   ...
   ...
});
0 Kudos
PeterHunt3
Deactivated User

Thanks Kristian, I shall give your suggestions a try.

0 Kudos
PeterHunt3
Deactivated User

I tried your suggestions Kristian, but still got the same issue.

I did some further investigation into the SEC7120 issue and I was able to resolve it by adding the entry beow in the proxy Web.config.  This initially fixed my issue.  Unfortunately I still do not get my layer consistently added.  I now get an SEC7123 error (see below).

<system.webServer>

   <httpProtocol>

     <customHeaders>

       <add name="Access-Control-Allow-Origin" value="*" />

     </customHeaders>

   </httpProtocol>

</system.webServer>

SEC7123: Request header content-type was not present in the Access-Control-Allow-Headers list.

0 Kudos
ChrisSmith7
Honored Contributor

Peter,

I've stumbled across some issues with the proxy in the past - if you run into any further issues, have a look at the following, if helpful:

Basic Viewer Print Widget in IE

0 Kudos