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
Yingruoyuan
V2EX  ›  Python

针对 ajax 的 post 请求,怎样转化为 Python 版?

  •  
  •   Yingruoyuan · 2017-05-08 14:06:57 +08:00 · 2222 次点击
    这是一个创建于 2542 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原网页中的动作为:

    value = 0
    $('input[name=value]').val(value);
    $.post('/test/', $('form.test').serialize())
    

    这种动作; $('form.test').serialize()这个打印出来是 "testid={}&testvalue={}&tocken={}"这种类型的

    我用 python 做:

    headers = {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
        'Cookie': 'uuid=123456;',
    }
    
    
    params = "testid={}&testvalue={}&token={}".format(testid, testvalue, token)
    
    resp = requests.post(url=url, data=params, headers=headers)
    
    print(resp)
    

    每次 response 返回的都是状态码 400,参数错误,我修改了 data 的格式为 json,字典,str 等参数格式,都不行,找不到解决的方向了,请问有人知道解决的方法吗?

    2 条回复    2017-05-08 14:29:08 +08:00
    Yingruoyuan
        2
    Yingruoyuan  
    OP
       2017-05-08 14:29:08 +08:00
    @hcymk2 谢谢,是我看 post 方法的时候没看仔细,修改为:
    ```
    params = {
    'testid': testid,
    'testvalue': testvalue,
    'token': token,
    }
    resp = requests.post(url=url, json=json.dumps(params), headers=headers)
    ```
    就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3403 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:44 · PVG 19:44 · LAX 04:44 · JFK 07:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.