1. 程式人生 > >ArcGIS API for Javascript proxy的配置

ArcGIS API for Javascript proxy的配置

為什麼要使用代理proxy?

答:未配置代理時server傳送請求是用get的方式,請求跨域訪問或者URL長度超過瀏覽器限制時,這就需要一個代理檔案proxy來轉發請求。Arcgis Server 10.0 由於不支援CORS,需要配置代理才能在前端js 程式中範圍進行操作,而有時候未配置代理也未出錯,例如:用extent進行空間查詢 後不報錯是因為選擇extent 請求的引數字串較短,jsapi 可以通過jsonp方法繞過跨域限制,但是一旦前端引數較多,例如用circle進行空間查詢,可以就無法通過jsonp跨域,這個時候如果要使用 10.0的 Identify task 就必須配置代理;還有Arcgis Server 10.2的列印功能可能也需要配置代理才行。

 

下載地址:https://github.com/Esri/resource-proxy/releases(注意:不同版本的Arcgis Server應下載對應版本的proxy檔案,而該頁面中的V1.1.0版本其實是支援10.2和10.3版本的,V1.0版本支援10.0版本,下載後解壓可以看到有3中型別:DotNet、Java、PHP的代理proxy檔案,此處以Java為例)

 

修改檔案:

V1.1.0版本:該檔案下載下來後需要修改proxy.jsp內容,全域性搜尋“<>”將對應的型別寫入,例如:檔案中有HashMap<>,未寫型別,可能就需要修改為HashMap<String,String>;還需要修改檔案“WEB-INF/classes/proxy.config”檔案中的“serverUrl”標籤的url屬性,修改為要呼叫Arcgis Server的地址,例如:“http://192.168.202.143:9797/arcgis/rest/services”)

 

(備註:若有多個不同域的服務地址,則需配置多個不同域的服務地址)

 

V1.0版本:不需要修改proxy.jsp內容,但該版本也需要修改 “WEB-INF/Classes/proxy.config”檔案,修改同V1.1.0版本一致。

 

如何部署?

修改完成後,將“Java"整個檔案拷貝到Tomcat的webapps檔案下,啟動Tomcat。

 

如何測試是否成功?

訪問:http:// 192.168.0.207:9391/Java/proxy.jsp?ping,會看到:

{ "ProxyVersion": "1.1.0", "Configuration File":"OK", "Log File": "OK"},然後再訪問:http:// 192.168.0.207:9391/Java/proxy.jsp?http://192.168.202.143:9797/arcgis/rest/services/?f=pjson,如果看到指定Arcgis Server目錄下的json資料,如

{"currentVersion":10.21,"folders":["Utilities","XKMSLFH"],"services":[{"name":"DOM2013","type":"ImageServer"}]},則到目前為止,代理配置成功!

 

如何在專案中使用?

Inorder for your application to route requests through the proxy you must addcode to your application to define the location of the proxy. If all requestsin your application will use the same proxy you can specify the proxy locationusing proxyUrl. You can also specify whether or not the proxy should always beused for communication using alwaysUseProxy

下面2行程式碼必須在專案中出現才能使該專案使用代理:

esri.config.defaults.io.proxyUrl= "http://192.168.0.207:9391/Java/proxy.jsp";

esri.config.defaults.io.alwaysUseProxy= false;

 

In the code above,esriConfig refers to the object returned by the esri/config module.

Another option isto setup a proxy rule that defines the proxy for a set of resources with the sameURL prefix. If the request URL matches a rule, then the request will be routedthrough the proxy. To define a proxy rule specify the url for the proxy and theprefix for the resources that need to be accessed through the proxy.

下面的程式碼可不用出現在專案中,不知道具體有什麼影響:

esri.urlUtils.addProxyRule({

       urlPrefix:"route.arcgis.com",

        proxyUrl:"http://192.168.0.207:9391/Java/proxy.jsp"

});

 

代理的配置幫助參考:https://developers.arcgis.com/javascript/jshelp/ags_proxy.html

好像下面這2個地址說的配置方式都有點複雜【10.0版本server配置代理:

http://help.arcgis.com/zh-cn/arcgisserver/10.0/help/arcgis_server_dotnet_help/index.html#//00930000009q000000

10.2版本server配置代理:

http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#/Using_a_forward_proxy_server_with_ArcGIS_Server/015400000309000000/