基于jquery也可以实现jsonp方式的ajax,方式如下:

(一)客户端

1、通过ajax中的dataType,设置跨越方式为jsonp,

2、默认执行get方式,不需额外指定,

3、jsonp,指定服务器端获取回调函数名称的参数名,

4、jsonpCallback,指定回调函数名称,

5、设置回调函数,

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <input type="button" value="jqueryJsonpAjax" onclick="JsonpAjax();" />
    <script src="sss/jquery-1.12.4.min.js"></script>
    <script>
        function JsonpAjax(){
            $.ajax({
                url:'http://ajax2.com:8888/index',
                dataType:'jsonp',
                jsonp:'callback',           <!--被请求端根据callback可以得到jsonpCallback设置的回调函数名称run-->
                jsonpCallback:'run',        <!--指明回调函数的名称run-->
            });
        };
        function run(arg){
            console.log(arg);
        };
    </script>
</body>
</html>
 Code
py
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

    def post(self, *args, **kwargs):
        self.write('t1.post')

# 路径解析
settings = {
    "template_path":"views",
    "static_path":"statics",
    "static_url_prefix":"/sss/",
}

# 二级路由,先匹配域名,
application = tornado.web.Application([
    (r"/index",IndexHandler),
],**settings)


# 开启服务器,监听
if __name__ == "__main__":
    application.listen(8001)
    tornado.ioloop.IOLoop.instance().start()
 Code

(二)服务器端

1、通过callback获取回调函数的名称,

2、对返回数据进行组合,并返回,

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        name = self.get_argument('callback')           # 获取客户端回调函数名称
        value = 'anything you want to return!'
        self.write("%s('%s');"%(name,value))             # 按客户端指定格式,返回数据

# 路径解析
settings = {
    "template_path":"views",
    "static_path":"statics",
    "static_url_prefix":"/sss/",
}

# 二级路由,先匹配域名,
application = tornado.web.Application([
    (r"/index",IndexHandler),
],**settings)


# 开启服务器,监听
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
View Code