推荐学习书目
› 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 · Aug 1, 2012 · 3431 views
    This topic created in 5167 days ago, the information mentioned may be changed or developed.
    我定义了一个视图用于处理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 replies  •  1970-01-01 08:00:00 +08:00
    suckli
        1
    suckli  
       Aug 1, 2012
    unicode(str(ok))
    这一句有问题,应该是str(ok)的时候出错的
    记住
    在python里面只有两种编码
    str和unicode
    试试把str(ok)这个抓换去掉
    stackpop
        2
    stackpop  
    OP
       Aug 1, 2012
    @suckli 我去掉了似乎还是有一样的问题,ok是整数0,1
    keakon
        3
    keakon  
       Aug 1, 2012
    先确定是不是unicode(str(ok))出错,再看看ok是什么…
    suckli
        4
    suckli  
       Aug 1, 2012
    把可以的数值用type()打印出来
    你确定ok是整数0,1吗?
    @stackpop
    fanzeyi
        5
    fanzeyi  
       Aug 1, 2012
    试试

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

    = =

    其实我觉得 unicode(int(ok)) 就够了..
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2508 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:42 · PVG 17:42 · LAX 02:42 · JFK 05:42
    ♥ Do have faith in what you're doing.