• CORS实现跨域Ajax


    客户端

    #!/usr/bin/env python
    import tornado.ioloop
    import tornado.web
    
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self, *args, **kwargs):
            self.render('index.html')
        def post(self, *args, **kwargs):
            self.render('index.html')
    
    class CorsHandler(tornado.web.RequestHandler):
        def get(self, *args, **kwargs):
            self.render('index.html')
        def post(self, *args, **kwargs):
            self.render('index.html')
    settings ={
        'template_path':'views',#html文件模板路径配置
        'static_path':'statics',#css,js文件路径配置
        'static_url_prefix':'/sss/',
    }
    application = tornado.web.Application([
            (r"/index", MainHandler),
        (r"/cors", CorsHandler),
            ],**settings)
    if __name__ == "__main__":
        application.listen(8000)
        tornado.ioloop.IOLoop.instance().start()
    app.py
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
         <input type="button" value="Jsonp" onclick="JsonpAjax();">
        <input type="button" value="DOJsonp" onclick="DoJsonpAjax();">
        <script src="{{static_url('jquery-3.1.1.js')}}"></script>
        <script>
    /
             function DoJsonpAjax() {
                $.ajax({
                url:'http://tao.com:8001/cors',
                type:'POST',
                data:{'k1':'v1'},
                success:function (arg) {
                    console.log(arg)
                }
            })
            }
            function JsonpAjax() {
                $.ajax({
                url:'http://tao.com:8001/cors',
                type:'PUT',
                headers:{'h1':'HH1'},
                data:{'k1':'v1'},
                xhrFields:{withCredentials:true},
                success:function (arg) {
                    console.log(arg)
                }
            })
            }
    //        function DoJsonpAjax() {
    ////            var tag = document.createElement('script');
    ////            tag.src = "http://www.jxntv.cn/data/jmd-jxtv2.html?callback=list";
    ////            document.head.appendChild(tag);
    ////
    ////            document.head.removeChild(tag);
    //////
    //            $.ajax({
    //                url: "http://tao.com:8001/index",
    //                type:"POST",
    //                dataType: 'jsonp',
    //                jsonp:'callback',//相当于在url: "http://tao.com:8001/index?callback= xxoo",
    //                jsonpCallback: "xxoo"//相当于在url: "http://tao.com:8001/index?callback= xxoo",
    //            })
    //        }
        </script>
    </body>
    </html>
    index.html

    服务端

    #!/usr/bin/env python
    import tornado.ioloop
    import tornado.web
    
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self, *args, **kwargs):
            callback = self.get_argument('callback')
            self.write("{}([11,22,33])".format(callback))
        def post(self, *args, **kwargs):
            self.write('t2.post')
    class CorsHandler(tornado.web.RequestHandler):
        def get(self, *args, **kwargs):
            self.write('index.get')
        def post(self, *args, **kwargs):
            self.set_header("Access-Control-Allow-Origin","http://taochen.com:8000")
            self.write('index.post')
        def options(self, *args, **kwargs):
            self.set_header("Access-Control-Allow-Origin", "http://taochen.com:8000")
            self.set_header("Access-Control-Allow-Methods", "PUT,DELETE")
            self.set_header("Access-Control-Allow-Headers", "h1")
            self.set_header("Access-Control-Allow-Credentials",'true')
        def put(self, *args, **kwargs):
            print(self.cookies)
            self.set_cookie('kk','kkk2')
            self.set_header("Access-Control-Allow-Origin", "http://taochen.com:8000")
            self.set_header("Access-Control-Allow-Credentials", 'true')
            self.write("ok")
    settings ={
        'template_path':'views',#html文件模板路径配置
        'static_path':'statics',#css,js文件路径配置
        'static_url_prefix':'/sss/',
    }
    application = tornado.web.Application([
            (r"/index", MainHandler),
        (r"/cors", CorsHandler),
            ],**settings)
    if __name__ == "__main__":
        application.listen(8001)
        tornado.ioloop.IOLoop.instance().start()
    app.py
  • 相关阅读:
    获取iframe中的元素
    用npm安装express后express命令找不到
    Openfire 单人聊天和多人聊天(发送消息、接收消息)
    openfire拦截数据包与发送广播
    xmpp with openfire 插件-利用Broadcast实现群
    Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
    openfire默认数据库与应用系统数据库整合
    ios即时通讯客户端开发之-mac上安装MySQL
    ios即时通讯客户端开发之-mac上搭建openfire服务器
    IOS block使用中碰到的一个小坑
  • 原文地址:https://www.cnblogs.com/shiluoliming/p/6567429.html
Copyright © 2020-2023  润新知