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

如何把 js 中的数据传到 Python 中

  •  
  •   yearking · 2018-08-20 17:18:07 +08:00 · 2559 次点击
    这是一个创建于 2047 天前的主题,其中的信息可能已经有所发展或是发生改变。
    10 条回复    2018-08-23 16:20:13 +08:00
    yearking
        1
    yearking  
    OP
       2018-08-20 17:18:54 +08:00
    正在学 python,如果可以,请给我一个案例学习。
    throns
        2
    throns  
       2018-08-20 17:24:43 +08:00 via Android   ❤️ 1
    yamedie
        3
    yamedie  
       2018-08-20 17:27:31 +08:00
    python 写一个 rest 接口供 js 调用就好了
    yearking
        4
    yearking  
    OP
       2018-08-20 17:30:49 +08:00
    问题没有描述清楚,我重新描述一下。
    我在 data.js 中定义了 var huilv = document.getElementById("huilv666").value;,如何在 main.py 中获取 data.js 中的 huilv ?
    yixiugegegege
        5
    yixiugegegege  
       2018-08-20 17:39:37 +08:00
    你这个问题的本质是 前端 怎么向 py 后端 传送数据
    前端一般是用 ajax
    py 后端最好用一个框架比如 tornado ,django 等 ,启动一个服务,
    伪代码:
    def main():
    if request.metod == 'POST'
    yixiugegegege
        6
    yixiugegegege  
       2018-08-20 17:40:15 +08:00   ❤️ 1
    太监了,理解一下
    airdge
        7
    airdge  
       2018-08-20 17:46:44 +08:00
    phantomjs
    a476286557
        8
    a476286557  
       2018-08-21 17:03:46 +08:00
    前端一般 ajax 即可。可以百度 ajax 和 flask 等框架传递 json。里面有详细的例子
    aniua
        9
    aniua  
       2018-08-23 16:19:03 +08:00
    你需要用随便哪个 web 框架起个 http 服务,来接收 http 请求。如 flask,最简单的情况是
    from flask import Flask, request
    app = Flask(__name__)

    @app.route('/', methods=['POST'])
    def index():
    data = request.json()
    # blabla 写你自己的逻辑然后 return

    app.run()
    aniua
        10
    aniua  
       2018-08-23 16:20:13 +08:00
    页面则用 ajax 发起请求
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3509 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:51 · PVG 18:51 · LAX 03:51 · JFK 06:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.