【问题标题】:What is the Python equivalent to PHP's $_GET?什么是 Python 等价于 PHP 的 $_GET?
【发布时间】:2013-09-26 16:26:18
【问题描述】:

我将值从 jQuery 的 .getJSON() 传递到 PHP 文件:

jQuery

<script>
function loadContent(href){
    $.getJSON("http://path/to/file.php", {cid: href, format: 'json'}, function(results){
        $("#my_div").html("");
        $.each(results, function(key,value){
            $("#my_div").append(value.content);
        });
    });
}
</script>

PHP

$href = $_GET['cid'];

这意味着我可以处理通过 PHP 文件中的 $_GET 从 jQuery 传递到 PHP 文件的 href 值。

我已将我的应用程序切换到 Python 并想知道 Python 等价物是什么:

$href = $_GET['cid'];

新环境是 MongoDB、PyMongo 和 Bottle。我相信瓶子正在使用wsgi。

我看到了一些对类似实现的引用:

href = request.GET['cid']

这会是最好的方法吗/这行得通吗(很难测试)?

更新:以上不起作用。

【问题讨论】:

  • 谁能建议我如何测试 Python 文件是否从 jQuery 中获取值?我不能从 Python 文件中alert,有没有等价的?
  • 你不能写一个 jquery 调用,只是 alert() 响应?并让它返回您阅读的内容?
  • 为此,我需要将href 的值返回给jQuery,这意味着我需要以某种方式将其编码为JSON,但我不知道该怎么做。我正在使用import json、json_string = json.dumps(href),然后是return json_string,但Firebug 将整个Python 文件显示为response 和content type 的text/plain。因为我不确定以下内容是否正确:href = request.GET['cid'],我不确定这本身是否会导致数据不返回给 jQuery。
  • 请贴出你目前得到的瓶子代码。

标签: php mongodb python-2.7 pymongo bottle


【解决方案1】:

解决办法是:

from bottle import response, request
href = request.GET.cid # where 'cid' is the key name of the value you passed in, see original post for that implementation.  

如果您想以 JSON 格式将数据发送回 jQuery,您可以这样做:

from bottle import response, request
from bson.json_util import dumps
href = request.GET.cid
# do things with variable here
response.content_type = 'application/json'
return dumps(your_variable)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2010-09-18
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多