首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用带有web.py后端的Pyjamas/PyJ的JSON调用

使用带有web.py后端的Pyjamas/PyJ的JSON调用
EN

Stack Overflow用户
提问于 2013-03-14 13:36:38
回答 1查看 725关注 0票数 0

这已经花了我很多小时的谷歌搜索,我仍然无法让它工作,时间所以要求帮助:)

我试图构建一个简单的测试应用程序,其中前端是用Pyjamas编写的,后端运行在Web.py上。他们应该通过JSON相互交谈。所需的功能是让用户输入一个字符串,然后将字符串转换为大写。

在Pyjamas在线书“世界其他地方”中有一个关于如何使用JSON的描述,它将各种技术混合在一起,因此很难解析。通过查看提示,例如来自Amund Tveit的博客等的提示,我拼凑了以下内容:

1) 服务器脚本,它非常简单:

代码语言:javascript
复制
import web
import json
import os

urls = (
    '/json', 'JSONHandler',
    '/json/', 'JSONHandler',
)
app = web.application(urls, globals())

class JSONHandler:
    def json_upper(self,args):
        return [args[0].upper()]

    def json_markdown(self,args):
        return [args[0].lower()]

    def POST(self):
        args = json.loads(web.data())
        print args
        json_func = getattr(self, 'json_%s' % args[u"method"])
        json_params = args[u"params"]
        json_method_id = args[u"id"]
        result = json_func(json_params)
        # reuse args to send result back
        args.pop(u"method")
        args["result"] = result[0]
        args["error"] = None # IMPORTANT!!
        web.header("Content-Type","text/html; charset=utf-8")
        return json.dumps(args)

if __name__ == "__main__":
    app.run()

而且绝对可以工作,使用依赖于Josh Marshall的JSON-RPC库的简单查询脚本(未显示)进行测试。

2)用于Pyjamas的客户端脚本,它也很简单:

代码语言:javascript
复制
# Client example from Amund Tveit's blog
# http://amundblog.blogspot.co.at/2008/12/ajax-with-python-combining-pyjs-and.html

# note: ui and JSONService were not prefixed with pyjamas, but that's needed
from pyjamas.ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel, HorizontalPanel, ListBox
from pyjamas.JSONService import JSONProxy

class Client:
    def onModuleLoad(self):
        self.TEXT_WAITING = "Waiting for response..."
        self.TEXT_ERROR = "Server Error"

        # This is the remote service
        self.remote_server = UpperService()

        self.status=Label()
        self.text_area = TextArea()
        self.text_area.setText(r"Please uppercase this string")
        self.text_area.setCharacterWidth(80)
        self.text_area.setVisibleLines(8)
        self.button_py = Button("Send to Python Service", self)
        buttons = HorizontalPanel()
        buttons.add(self.button_py)
        buttons.setSpacing(8)
        info = r'Upper-case a string using JSON-RPC'
        panel = VerticalPanel()
        panel.add(HTML(info))
        panel.add(self.text_area)
        panel.add(buttons)
        panel.add(self.status)
        RootPanel().add(panel)

    def onClick(self, sender):
        self.status.setText(self.TEXT_WAITING)
        text = self.text_area.getText()
        # (data, response_class): if the latter is 'self', then
        # the response is handled by the self.onRemoteResponse() method
        if self.remote_server.upper(self.text_area.getText(), self) < 0:
            self.status.setText(self.TEXT_ERROR)

    def onRemoteResponse(self, response, request_info):
        self.status.setText(response)

    def onRemoteError(self, code, message, request_info):
        self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message)

# AJAX calls must come from the same server, only the path is given here
class UpperService(JSONProxy):
    def __init__(self):
        JSONProxy.__init__(self, "/json/", ["upper"])

我用PyJ编译了它,将默认的output目录重命名为static,以便web.py可以为它服务,编辑了static/Client.html,以便内部引用指向static

代码语言:javascript
复制
<html>
<!-- auto-generated html - You should consider editing and adapting this
 to suit your requirements. No doctype used here to force quirks mode; see
 wiki for details: http://pyjs.org/wiki/csshellandhowtodealwithit/
-->
<head>

<title>Client (Pyjamas Auto-Generated HTML file)</title>
<meta name="pygwt:module" content="/static/Client"> <!-- was content="Client" -->
</head>
<body style="background-color:white">
<script type="text/javascript" src="/static/bootstrap.js"></script> <!-- was src="bootstrap.js" -->
<iframe id="__pygwt_historyFrame" style="display:none;"></iframe>
</body>
</html>

..。然后将浏览器指向http://localhost:8080/static/Client.html。我得到的只是一个空白页,检查页面源代码显示了上面的static/Client.html,因此它确实被提供给了浏览器。服务器的日志还显示,至少有一些页面已被服务:

代码语言:javascript
复制
http://0.0.0.0:8080/
127.0.0.1:61466 - - [14/Mar/2013 13:59:39] "HTTP/1.1 GET /static/Client.html" - 200 
127.0.0.1:61466 - - [14/Mar/2013 13:59:40] "HTTP/1.1 GET /static/Client.nocache.html" - 200 
127.0.0.1:61466 - - [14/Mar/2013 13:59:40] "HTTP/1.1 GET /static/Client.safari.cache.html" - 200 

然而,没有迹象表明出了什么问题。尝试了各种其他的URL组合,重命名目录,用-d选项编译Pyjamas部分,希望得到一个调试堆栈跟踪.都没有用。

有人成功地让睡衣和Web.py一起工作了吗?如果是,那么请分享如何。谢谢。

PS:我正在使用web.py V0.37和最新的Pyjamas开发版本。(当前的稳定版本V0.8.1也不能工作。)

EN

回答 1

Stack Overflow用户

发布于 2013-06-18 22:37:24

我知道您可能已经忘记了这一点,但是找到您的代码帮助我修复了我的代码,而且在网上为webpy的pyjs找到工作示例似乎很棘手。

您的问题在客户端脚本端,您需要添加一些代码来启动客户机:

代码语言:javascript
复制
if __name__ == '__main__':
    app = Client()
    app.onModuleLoad()

此外,为了避免随后出现的错误,您的第一行导入应该更改为:

代码语言:javascript
复制
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.TextArea import TextArea
from pyjamas.ui.Label import Label
from pyjamas.ui.Button import Button
from pyjamas.ui.HTML import HTML
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.ListBox import ListBox
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15410855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档