zoukankan      html  css  js  c++  java
  • Jsonp实现跨域请求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')
    
    
    
    settings ={
        'template_path':'views',#html文件模板路径配置
        'static_path':'statics',#css,js文件路径配置
        'static_url_prefix':'/sss/',
    }
    application = tornado.web.Application([
            (r"/index", MainHandler),
    
            ],**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="DoJsonpAjax();">
        <script src="{{static_url('jquery-3.1.1.js')}}"></script>
        <script>
             function xxoo(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",
                    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')
    
    
    
    settings ={
        'template_path':'views',#html文件模板路径配置
        'static_path':'statics',#css,js文件路径配置
        'static_url_prefix':'/sss/',
    }
    application = tornado.web.Application([
            (r"/index", MainHandler),
    
            ],**settings)
    if __name__ == "__main__":
        application.listen(8001)
        tornado.ioloop.IOLoop.instance().start()
    app.py
  • 相关阅读:
    Java获取Linux系统cpu使用率
    jsoup 提取 html 中的所有链接、图片和媒体
    使用Spring定时任务并且通过AOP监控任务执行情况
    MySQL导出数据库、数据库表结构、存储过程及函数【用】
    linux下部署一个JavaEE项目的简单步骤
    MySQL 错误日志(Error Log)
    linux下程序JDBC连接不到mysql数据库
    linux下mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”)的处理方法
    Spring Boot的核心
    项目中菜单折叠问题
  • 原文地址:https://www.cnblogs.com/shiluoliming/p/6567396.html
Copyright © 2011-2022 走看看