1. 程式人生 > >關於 webpy跨域 解決方法的一點總結

關於 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()