arcgis api for JavaScript 跨域

错误消息提示:
XMLHttpRequest cannot load http://10.32.2.70:8399/ArcGIS/rest/info?f=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:58374' is therefore not allowed access.

跨域:http://blog.csdn.net/lambert310/article/details/51683775

解决方案:

参考https://developers.arcgis.com/javascript/jshelp/inside_defaults.html
esriConfig.defaults.io.corsEnabledServers这一段,把要访问的服务对应的server地址添加进去:

require(["esri/config"], function(esriConfig) {
esriConfig.defaults.io.corsEnabledServers.push("localhost:6080");
});

如上代码所示,localhost:6080 即 server 服务所在arcgis server 地址。

以上为 arcgis api for JavaScript 3.x 所使用的方法,arcgis api for JavaScript 4.x 修改为:
参考https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html

其中的 corsEnabledServers 这一段
Example:

// Add a known server to the list.
require(["esri/config"], function(esriConfig) {
  esriConfig.request.corsEnabledServers.push("localhost:6080");
});

实际代码如下图所示(以 4.x 版本为例):


如果还不行就需要配置代理,参考https://developers.arcgis.com/javascript/jshelp/ags_proxy.html
帮助文档 :http://enterprise.arcgis.com/zh-cn/server/latest/administer/linux/restricting-cross-domain-requests-to-arcgis-server.htm
建议尝试,根据帮助文档的操作步骤 将 “http://localhost” 加入AllowedOrigins 字段中

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。