V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
stackpop
V2EX  ›  Python

python的ajax求助。。。

  •  
  •   stackpop ·
    sjtubinlong · 2012-08-01 19:07:42 +08:00 · 2640 次点击
    这是一个创建于 4284 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我定义了一个视图用于处理post过来的ajax请求
    def upload(request):
    url = request.POST.get('image_url')
    ats = request.POST.get('check')
    ok = renren_upload(request.session['session_key'],url,ats)
    return httpResponse(unicode(str(ok)))

    其中ok是另外一个函数的返回值,是0和1

    但是老是不成功,提示

    UnicodeDecodeError at /upload/
    'ascii' codec can't decode byte 0xff in position 2: ordinal not in range(128)

    我试图把ok转化为unicode也不行啊。。。。

    这是为啥呢?
    5 条回复    1970-01-01 08:00:00 +08:00
    suckli
        1
    suckli  
       2012-08-01 19:31:54 +08:00
    unicode(str(ok))
    这一句有问题,应该是str(ok)的时候出错的
    记住
    在python里面只有两种编码
    str和unicode
    试试把str(ok)这个抓换去掉
    stackpop
        2
    stackpop  
    OP
       2012-08-01 19:46:11 +08:00
    @suckli 我去掉了似乎还是有一样的问题,ok是整数0,1
    keakon
        3
    keakon  
       2012-08-01 20:00:25 +08:00
    先确定是不是unicode(str(ok))出错,再看看ok是什么…
    suckli
        4
    suckli  
       2012-08-01 20:36:54 +08:00
    把可以的数值用type()打印出来
    你确定ok是整数0,1吗?
    @stackpop
    fanzeyi
        5
    fanzeyi  
       2012-08-01 20:38:57 +08:00
    试试

    unicode(str(int(ok)))
    ....

    = =

    其实我觉得 unicode(int(ok)) 就够了..
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5182 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 09:22 · PVG 17:22 · LAX 02:22 · JFK 05:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.