【发布时间】:2012-02-23 23:41:22
【问题描述】:
快速摘要:我正在创建一个 Chrome 扩展程序,它与 App Engine 上的 Python 2.5 运行时应用程序对话,该应用程序查询 Google Cloud SQL 中的 SQL 数据库。 App Engine 教程主要是入门教程,我找不到任何关于如何正确处理应用程序的 AJAX 帖子的教程。
- 设计:Chrome 扩展输入创建 AJAX 帖子 > Python 应用程序 > SQL DB。
- Python 运行时:2.5
- 主机:App Engine 托管应用,Cloud SQL 托管 SQL 数据库
Chrome 扩展程序正在进行 AJAX 调用,但 App Engine 上没有任何反应。
当直接从它的 URL 运行 Python 应用程序时(我有允许我直接向搜索类提交变量的测试代码)它能够返回正确的结果,因此查询和数据库连接工作。
我遇到的问题是:
(a) 不确定我的 AJAX 请求是否命中了 Python 应用程序
(b) 不确定我是否正确处理了 AJAX 请求(即从中读取数据并响应输出)
我已经在网上翻阅了文档和示例,但找不到真正概述如何获取 AJAX 查询以与 AppEngine 上托管的 Python 应用程序交互的文档和示例。如果有人发现任何明显的错误或可以向我指出相关文档,我将不胜感激!
Chrome 扩展 HTML
<head>
<script src='jquery-1.5.1.js'></script>
<script type="text/javascript">
function storeInput(value) {
$.ajax({
url: '<url>'
type: 'POST',
data: {'term': value},
dataType: 'text',
success: function (data) {
console.log('boom: ', data);
//var response = '<p class="desc_line"><span class="desc_title">Name: </span><span class="desc_text">' + eval(data) + '</p>';
},
error: function(data) {
console.log('no chance');
}
});
}
$(function() {
//when the page loads
$('.input-form').live('submit', function(e) {
e.preventDefault();
var formInput = $(this).find('.term-input').val();
storeInput(formInput);
});
});
</script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
App Engine Python 代码
from __future__ import with_statement
import cgi
import urllib
from google.appengine.api import rdbms
from google.appengine.api import files
from google.appengine.ext import db
from google.appengine.ext import webapp
from django.utils import simplejson
from google.appengine.ext.webapp.util import run_wsgi_app
_INSTANCE_NAME = '<instance>'
def Parse
# redacted since this works fine to work through strings passed to it and turn them into proper SQL queries.
class Search(webapp.RequestHandler):
def post(self):
#Create connection to database
conn = rdbms.connect(instance=_INSTANCE_NAME, database='salesinfo')
cursor = conn.cursor()
# ideally set the body of the AJAX call to a variable but thats not working
user_input = self.request.body
# Parse input
sql_query = []
for value in Parse(user_input):
sql_query.append(value)
# Try first query
cursor.execute(sql_query[0])
# If first query yields no results, try the second query
if cursor.rowcount < 1:
cursor.execute(sql_query[1])
for row in cursor.fetchall():
output = row[0]
self.response.out.write(output) # ideally respond with the result
conn.close()
application = webapp.WSGIApplication(
[('/', Search)], #removed request
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
【问题讨论】:
-
我不熟悉应用程序引擎,但你能放入调试器看看它是否正常运行吗?您是否尝试设置打印语句以查看它是否击中您的 python?您可以使用带有 chrome 扩展的 chrome 开发者工具查看 Web 请求吗?
标签: python google-app-engine jquery