推荐学习书目
› 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 · May 8, 2017 · 3213 views
    This topic created in 3427 days ago, the information mentioned may be changed or developed.

    原网页中的动作为:

    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 replies  •  2017-05-08 14:29:08 +08:00
    Yingruoyuan
        2
    Yingruoyuan  
    OP
       May 8, 2017
    @hcymk2 谢谢,是我看 post 方法的时候没看仔细,修改为:
    ```
    params = {
    'testid': testid,
    'testvalue': testvalue,
    'token': token,
    }
    resp = requests.post(url=url, json=json.dumps(params), headers=headers)
    ```
    就可以了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2319 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:08 · PVG 00:08 · LAX 09:08 · JFK 12:08
    ♥ Do have faith in what you're doing.