关于 webpy跨域 解决方法的一点总结

python3.6.1代码如下:

服务代码:

'''
此代码实现了:
arcgis api for js 3.25 服务的本地化。
此代码 + arcgis_js_v325_sdk 文件夹 是一套。

arcgis api for js 的其他版本,也一样。

启动代码:
python v325.py

测试URL:
http://localhost:8080/arcgis_js_v326_sdk/arcgis_js_api/library/3.26/3.26/dijit/themes/nihilo/nihilo.css
http://localhost:8080/arcgis_js_v326_sdk/arcgis_js_api/library/3.26/3.26/esri/css/esri.css
http://localhost:8080/arcgis_js_v326_sdk/arcgis_js_api/library/3.26/3.26/init.js


http://localhost:8080/arcgis_js_v325_sdk/arcgis_js_api/library/3.25/3.25/esri/css/esri.css
http://localhost:8080/arcgis_js_v325_sdk/arcgis_js_api/library/3.25/3.25/init.js

http://localhost:8080/
http://localhost:8080/2
'''
import web
urls = (
    '/', 'getJSON',
    '/2', 'getJSON2',
    '/(.*)', 'retrieve'
)

class retrieve:
    def GET(self, filename):                      
        file = 'C:/Users/asus/Desktop/gaoshengjie/localServers/' + filename        
        f = open(file, 'rb').read()
        return f


class getJSON:
    def GET(self):
        '''
        此方法适用于,前端类似这样的请求:
        $.getJSON("http://localhost:8080/", function (data) {
            // data, 即请求json文件的内容,且data为json格式。
        });
        '''
        web.header("Access-Control-Allow-Origin", "*")
        file = 'C:/Users/asus/Desktop/gaoshengjie/localServers/config2d.json'
        f = open(file, 'rb').read()
        return f


class getJSON2:
    '''
    此类中两种方法,适用于,首先有OPTIONS请求,然后有GET请求的场景。
    前端请求类似这样:
    // var configfile = haoutil.system.getRequestByName("config", "config.json");
    // var configfile = "./config.json";

    mars3d.createMap({
        id: 'cesiumContainer',
        // url: configfile + "?time=20180616",
        url: "http://localhost:8080/2?time=20180616",
        //infoBox: false,     //是否显示点击要素之后显示的信息  【也可以在config.json中配置】
        //shadows : true,

        layerToMap: layerToMap,
        success: function (_viewer, gisdata, jsondata) { //地图成功加载完成后执行
            viewer = _viewer;
            configdata = jsondata;
            
            layerDetails = jsondata.map3d

            mars3d.widget.init(_viewer, jsondata.widget);

            loaderOK();
            tree_3d();

            var request = haoutil.system.getRequest();

            //如果有xyz传参,进行定位
            if (haoutil.isutil.isNotNull(request.x) &&
                haoutil.isutil.isNotNull(request.y)) {
                viewer.mars.centerAt(request, {
                    duration: 0,
                    isWgs84: true
                });
            }

            entityentity = _viewer.entities.add({
                position: Cesium.Cartesian3.fromDegrees(117.179085, 39.058676, 20),
                billboard: {
                    image: './img/marker/mark1.png',
                    pixelOffset: new Cesium.Cartesian2(0, 0), // default: (0, 0)
                    scale: 0.5 // default: 1.0
                }
            });
            entityentity.popup = {
                html: '<div class="addmarker-popup-titile">添加标记</div><div class="addmarker-popup-content" ><form >' +
                    '<div class="form-group">  <label for="addmarker_attr_name">名称</label><input type="text" id="addmarker_attr_name" class="form-control" value="' + '' + '" placeholder="请输入标记名称"    /> </div>' +
                    '<div class="form-group">  <label for="addmarker_attr_remark">备注</label><textarea id="addmarker_attr_remark" class="form-control" rows="3" style="resize: none;" placeholder="请输入备注(可选填)"   >' + '' + '</textarea></div>' +
                    '<div class="form-group" style="text-align: center;"><input type="button" class="btn btn-primary  btn-sm" value="保存" onclick="" />' +
                    '&nbsp;&nbsp;<input type="button" class="btn btn-danger  btn-sm" value="删除" onclick="" /></div>' +
                    '</form></div>', //可以是任意html
                anchor: [0, -25]
            };

            entityentity.tooltip = {
                html: '<div class="addmarker-popup-titile">我的标记</div><div class="addmarker-popup-content" ><form >' +
                    '<div class="form-group"><label>名称</label>:' + '测绘院' + '</div>' +
                    '<div class="form-group"><label>备注</label>:' + '天津' + '</div>' +
                    '</form></div>', //可以是任意html
                anchor: [0, -25]
            };

            initWork(_viewer);

            // //开场动画
            viewer.mars.openFlyAnimation(function () {
                //动画播放完成后回调

                //如果url传参,激活对应widget
                if (haoutil.isutil.isNotNull(request.widget))
                    mars3d.widget.activate(request.widget);

                // initWork(_viewer);
            });
        }
    });
    '''
    def GET(self):
        # web.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization")
        # web.header("Access-Control-Allow-Methods", "GET, POST,PUT,DELETE,OPTIONS")
        web.header("Access-Control-Allow-Origin", "*")
        file = 'C:/Users/asus/Desktop/gaoshengjie/localServers/config.json'
        f = open(file, 'rb').read()
        return f

    def OPTIONS(self):
        web.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization")
        web.header("Access-Control-Allow-Origin", "*")
        file = 'C:/Users/asus/Desktop/gaoshengjie/localServers/config.json'
        f = open(file, 'rb').read()
        return f

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()
 

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/84437272
今日推荐