web.py header_跨域.py

"""
测试 URL:http://localhost:8080/http://localhost:8080/2d知识点:0.Help on function header in module web.webapi:web.header(hdr, value, unique=False)指定响应头。Adds the header `hdr: value` with the response.If `unique` is True and a header with that name already exists,it doesn't add a new one.1.web.headerweb.header('content-type', 'application/json; charset=utf-8')# web.header('content-type', 'text/json; charset=utf-8')指定响应头的内容类型及编码。可解决中文乱码。即,返回的数据类型,编码。在前端的话,就指发送到后端的数据类型。都是指要给对方的。web.header("Access-Control-Allow-Origin", "*")指定允许访问的域名,如:http://ip:port/web.header("Access-Control-Allow-Methods", "GET, POST,PUT,DELETE,OPTIONS")web.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization")
"""
import weburls = ('/2d', 'getConfig2d','/', 'getConfig'
)class getConfig2d:def GET(self):"""此方法适用于,前端类似这样的请求:$.getJSON("http://localhost:8080/2d", function (data) {// data, 即请求json文件的内容,且data为json格式。});"""web.header('content-type', 'text/json; charset=utf-8')web.header("Access-Control-Allow-Origin", "*")file = 'C:/Users/asus/Desktop/gaoshengjie/localServers/config2d.json'f = open(file, 'rb').read()return fclass getConfig:"""此类中两种方法,适用于,首先有OPTIONS请求,然后有GET请求的场景。前端请求类似这样:mars3d.createMap({id: 'cesiumContainer',// url: configfile + "?time=20180616",url: "http://localhost:8080/?time=20180616",//infoBox: false,     //是否显示点击要素之后显示的信息  【也可以在config.json中配置】//shadows : true,layerToMap: layerToMap,success: function (_viewer, gisdata, jsondata) { //地图成功加载完成后执行}});"""def GET(self):web.header("Access-Control-Allow-Origin", "*")file = 'C:/Users/asus/Desktop/gaoshengjie/localServers/config.json'f = open(file, 'rb').read()return fdef OPTIONS(self):"""预检请求。跨域请求中的非简单请求,需要设置 访问控制允许的请求头 和 方法。:return:"""web.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization")web.header("Access-Control-Allow-Methods", "GET, POST")if __name__ == "__main__":app = web.application(urls, globals())app.run()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部