快速總結:我正在創建一個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