Python - 从服务器获取数据 (ajax) - 'str' 对象不是可调用的错误

Python - get data from server (ajax) - 'str' object is not callable error

本文关键字:对象 调用 错误 str 服务器 数据 ajax Python 获取      更新时间:2023-09-26

我想我已经查看了谷歌中几乎所有的例子来解决这个问题,但仍然无法让它正常工作。

我的目标是在单击按钮后从服务器请求数据。
错误我此刻得到:

Traceback (most recent call last):
  File "/opt/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1546, in __call__
    return response(environ, start_response)
TypeError: 'str' object is not callable
INFO     2015-01-04 22:16:52,235 module.py:709] default: "POST /nearest_banks/radius HTTP/1.1" 500 662

我的代码:

AJAX 函数:

<script type="text/javascript">
    $( "#yes" ).click(function() {
        $( "#ask_user" ).hide();
        $.ajax({
            type: "post",
            url: '/nearest_banks/radius',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
           // data: { 'names': {{names}}, 'location': {{location}} },
            success: function (output) {
                output = JSON.parse(output);
                ("#places").empty();
                output.forEach(function (names) {
                    var $row = $('<p>').appendTo("#places");
                });
            }});
    });
</script>

蟒蛇函数:

def post(self):
        names, location, telephone, check = funcs.Findplaces(default_radius)  
        dict = {'names': names, 'location': location}
        output = json.dumps(dict)
        return output
  • 函数查找位置运行良好。它以 Unicode 类型的正确方式返回名称、位置等。

这些是我的网址:

application = webapp2.WSGIApplication([
   webapp2.Route(r'/', handler=MainPage, name='mpage', handler_method='get'),
   webapp2.Route(r'/nearest_banks', handler=Nearest_banks, name='ne_banks', handler_method='main_func'),
   #webapp2.Route(r'/nearest_banks/radius', handler=Nearest_banks, name='ne_banks', handler_method='get'),
   webapp2.Route(r'/nearest_banks/radius', handler=Nearest_banks, name='radius', handler_method='post'),
], config=session_module.config, debug=True)

据我了解,错误出在 Python def post 函数中。请帮我找人应付...

我所需要的只是从服务器获取数据并将其显示在页面上的某个标签中......

参见 http://webapp-improved.appspot.com/guide/handlers.html#returned-values

处理程序方法不需要返回任何内容:它可以简单地使用 self.response.write() 写入响应对象。

但是处理程序可以返回要在响应中使用的值。使用默认调度程序实现,如果处理程序返回任何不是 None 的内容,则它必须是webapp2.Response实例。如果这样做,则使用该响应对象而不是默认对象。

post()处理程序现在返回一个字符串。