【问题标题】:How to get data from jQuery script (web.py)?如何从 jQuery 脚本(web.py)中获取数据?
【发布时间】:2011-12-06 11:42:27
【问题描述】:

我找不到任何关于 jQuery + web.py 的教程。 所以我有关于 POST 方法的基本问题。

我有 jQuery 脚本:

<script>
     jQuery('#continue').click(function() {
         var command = jQuery('#continue').attr('value');
         jQuery.ajax({
              type: "POST",
              data: {signal : command},
         });
     });
</script>

一个简单的表格:

<form>
    <button type="submit">Cancel</button>
    <button type="submit" id="continue" value="next">Continue</button>
</form>

和python脚本:

def POST (self):
        s = signal
        print s
        return

我希望在控制台中看到字符串“next”。但它不会发生。

我有一种强烈的感觉,选择器不知何故不起作用。有什么方法可以检查吗?

【问题讨论】:

  • jQuery.ajax 需要一个 url 来发布/获取内容,所以在 type 和 data 旁边添加 url 参数

标签: jquery python ajax web.py


【解决方案1】:

你需要在 web.py 中使用 web.input 来访问 POST 变量

查看文档:http://webpy.org/docs/0.3/api(搜索“函数输入”)

def POST(self):
    s = web.input().signal
    print s
    return

【讨论】:

    【解决方案2】:
    <script>
         jQuery('#continue').click(function() {
             var command = jQuery('#continue').attr('value');
             jQuery.ajax({
                  type: "POST",
                  data: {signal : command},
                  url: "add the url here"
             });
         });
    </script>
    

    添加服务器的url。

    【讨论】:

    • 找到了这个例子:kooneiform.wordpress.com/2010/02/28/… - 它可以在没有 URL 的情况下工作。
    • 我从 jQuery 文档中了解到 - 当前页面是 URL 的默认设置,所以我可以忽略这一行
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多