【问题标题】:bottle.py: sending data via javascriptbottle.py:通过 javascript 发送数据
【发布时间】:2012-02-20 12:21:46
【问题描述】:

基本上,我希望在我的网站上有一个交互式按钮,当点击该按钮时,会向服务器发送一些数据,以便检查并显示响应(无需发送表单/重新加载页面)。

我以为会是这样的:

function checkData()
{
    var req = new XMLHttpRequest();
    var conf = document.getElementById('my_text_area').value;

    req.open("GET", 'check_data', true);
    req.onreadystatechange = function () 
    {
        var pre = document.getElementById('check_data_out');
        pre.innerHTML = req.responseText;
    }

    req.send(conf);
    return false;
}

在服务器端:

@get('/check_data')
def check_data():
    # Process the content and answer something...
    content = str(request.is_ajax) + ' - ' + str(request.GET) + ' - ' + str(request.POST)
    return content

但这显然行不通。要么不是通过 javascript 发送数据的正确方式,要么不是在 bottle.py 中访问数据的正确方式。

非常感谢向我展示它是如何工作的。

【问题讨论】:

  • 为什么不用dojo和jquery等javascript库呢?否则,您需要额外的努力来克服不同浏览器之间 AJAX 的差异。
  • 它不起作用怎么办?使用 Chrome 开发工具检查请求/响应。
  • 对不起,这个问题/问题现在太老了,我不记得它的细节了。

标签: javascript xmlhttprequest bottle


【解决方案1】:

您可以将 dojo 用于客户端逻辑。

var button = dojo.byId('button_id'); // button_id refers to the id of the button you want to click

dojo.connect(button,'onclick',dojo.xhrGet({
   url: '/check_data',
   handleAs : 'text',
   load : function(response){
       dojo.byId('button_id').innerHTML = response; 
   }
}));

【讨论】:

    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2014-11-20
    • 2013-01-31
    • 1970-01-01
    • 2014-04-06
    相关资源
    最近更新 更多