快速总结:我正在创建一个Chrome扩展,与App Engine上的Python 2.5运行时应用程序通信,该应用程序可以在Google Cloud SQL中查询SQL数据库。 App Engine教程主要是入门级的,我找不到任何关于如何正确处理应用程序的AJAX帖子的教程。如何使用webapp.RequestHandler处理Google App Engine Python 2.5上的AJAX POST?
- 设计:Chrome扩展输入创建一个AJAX Post> Python App> SQL DB。
- Python运行:2.5
- 主持人:App Engine的托管应用程序,云SQL主机SQL数据库
Chrome扩展正使AJAX调用,但什么也没发生在App Engine上。
当直接从它运行的Python应用程序的URL(我有测试代码,让我给一个变量直接提交给搜索类),它能够返回正确的结果,所以查询和数据库连接工作。
我遇到的问题是,我:
(一)不知道如果我的AJAX请求,甚至打到Python应用程序
(二)不知道,如果我处理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请求? – dm03514 2012-02-24 00:04:25