Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中如何将jsonp发送到javascript_Javascript_Python_Json_Mongodb_Tornado - Fatal编程技术网

在Python3中如何将jsonp发送到javascript

在Python3中如何将jsonp发送到javascript,javascript,python,json,mongodb,tornado,Javascript,Python,Json,Mongodb,Tornado,在这种情况下,我使用的是Tornado web 我只想使用一些简单的方法将json数据从mongodb发送到javascript。 我只是在网上查了一些例子。我很困惑 最后我得到了结果,Python 2网络允许您通过字符串发送消息 Python 3必须是字节 实际上这段源代码来自internet,但是由python 2编写的,无法在上运行 蟒蛇3 from tornado.web import RequestHandler # this Tornado standard class JSO

在这种情况下,我使用的是Tornado web
我只想使用一些简单的方法将json数据从mongodb发送到javascript。 我只是在网上查了一些例子。我很困惑 最后我得到了结果,Python 2网络允许您通过字符串发送消息 Python 3必须是字节

实际上这段源代码来自internet,但是由python 2编写的,无法在上运行 蟒蛇3

from tornado.web import RequestHandler  # this Tornado standard 

class JSONPHandler(RequestHandler):
    CALLBACK = 'jsonp' # define callback argument name <== this Javascript send to python callback name, java script send msg look like ?jsonp=?  check it 
    def finish(self, chunk=None):
        assert not self._finished
        if chunk: self.write(chunk)
        # get client callback method 
        print(type(self.CALLBACK)) <==show string class  
        callbacka = self.get_argument(self.CALLBACK)
        callback=bytes(callbacka+'(','utf-8') <== from this part to  new 
        # format output with jsonp
        self._write_buffer.insert(0,callback ) <== write some json head 
        self._write_buffer.append(bytes(')','utf-8'))  <== all msg must be bytes 
        super(JSONPHandler, self).finish()  <== must do finished step 
        # chunk must be None
来自tornado.web导入请求处理程序#此tornado标准
类JSONPHandler(RequestHandler):
CALLBACK='jsonp'#定义回调参数名称RequestHandler.write()和RequestHandler.finish()将为您将输入转换为utf8字节。首先,打开“mongo”shell并执行以下操作:

> use test
switched to db test
> db.collection.insert({key: 'value'})
> db.collection.find()
{ "_id" : ObjectId("53232a5c8d12c74bb1a30bc1"), "key" : "value" }
注意这里生成的ObjectId。下面是一个使用JSONP和PyMongo的代码示例:

import bson.json_util
import pymongo
from bson import ObjectId
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, HTTPError, Application

db = pymongo.MongoClient().test


class JSONPHandler(RequestHandler):
    def get(self):
        jsonp_callback_name = self.get_argument('jsonp')
        oid = self.get_argument('id')
        doc = db.collection.find_one(ObjectId(oid))
        if not doc:
            raise HTTPError(404)

        # bson.json_util handles nonstandard types like ObjectId.
        self.finish('%s(%s)' % (
            jsonp_callback_name,
            bson.json_util.dumps(doc)))

application = Application(
    [('/api', JSONPHandler)]
)

if __name__ == '__main__':
    application.listen(8888)
    IOLoop.current().start()
现在使用从“mongo”shell生成的ObjectId访问此URL:

您应该在浏览器中看到这样的输出:

mycallback({"_id": {"$oid": "53232a5c8d12c74bb1a30bc1"}, "key": "value"})
mycallback({"_id": {"$oid": "53232a5c8d12c74bb1a30bc1"}, "key": "value"})