要約:私は、Google Cloud SQLのSQLデータベースを照会するApp EngineのPython 2.5ランタイムアプリケーションと会話するChrome拡張機能を作成しています。 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から直接実行すると(私は変数を直接検索クラスにサブミットできるテストコードを持っています)、正しい結果を返すことができるため、クエリとデータベース接続が機能します。
私がいる問題は、私がいることである:
(a)は私のAJAX要求もPythonアプリケーションに当たっているかどうかわからない
(b)はわからない私は取り扱いしていた場合AJAXのリクエストが正しく行われている(つまり、データを読み込んで出力に応答する)
オンラインでドキュメントや例を紹介しましたが、AJAXクエリとの対話方法を実際に説明するものは見つかりませんでしたAppEngineのホストされたPythonアプリケーション。誰かが目障りなエラーを見た場合、または関連するドキュメントに私を指摘することができる場合、私は非常に感謝しています!ここ
クローム拡張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を打っているかどうかを確認するためにprintステートメントを設定しようとしましたか?クロム拡張機能を備えたクローム開発ツールを使用してウェブリクエストを確認できますか? – dm03514