Python - 从服务器获取数据 (ajax) - 'str' object is not callable 错误
Python - get data from server (ajax) - 'str' object is not callable error
我想我已经查看了 google 中的几乎所有示例来解决问题,但仍然无法正常工作。
我的目标是点击按钮后从服务器请求数据。
我此时得到的错误:
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>
Python 函数:
def post(self):
names, location, telephone, check = funcs.Findplaces(default_radius)
dict = {'names': names, 'location': location}
output = json.dumps(dict)
return output
- 函数 Findplaces 运行良好。它 returns 名称、位置等以 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
A handler method doesn't need to return anything: it can simply write to the response object using self.response.write()
.
But a handler can return values to be used in the response. Using the default dispatcher implementation, if a handler returns anything that is not None it must be a webapp2.Response
instance. If it does so, that response object is used instead of the default one.
您的 post()
处理程序现在正在返回一个字符串。
我想我已经查看了 google 中的几乎所有示例来解决问题,但仍然无法正常工作。
我的目标是点击按钮后从服务器请求数据。
我此时得到的错误:
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>
Python 函数:
def post(self):
names, location, telephone, check = funcs.Findplaces(default_radius)
dict = {'names': names, 'location': location}
output = json.dumps(dict)
return output
- 函数 Findplaces 运行良好。它 returns 名称、位置等以 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
A handler method doesn't need to return anything: it can simply write to the response object using
self.response.write()
.But a handler can return values to be used in the response. Using the default dispatcher implementation, if a handler returns anything that is not None it must be a
webapp2.Response
instance. If it does so, that response object is used instead of the default one.
您的 post()
处理程序现在正在返回一个字符串。