4

Someone has to be able to explain what I'm doing wrong here! I'm trying to create the MOST simple example of an AJAX post to a Google App Engine app...and I'm failing!

Here is the app python


import cgi from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import db from django.utils import simplejson class EmailItem(db.Model): email = db.StringProperty(multiline=False) date = db.DateTimeProperty(auto_now_add=True) class EmailList(webapp.RequestHandler): def get(self): self.response.out.write("You see nothing!") def post(self): eitem = EmailItem() eitem.email = self.request.get("address") eitem.put() self.response.out.write("success") application = webapp.WSGIApplication([('/', EmailList)]) def main(): run_wsgi_app(application) if __name__ == "__main__": main() 

And here's the jQuery


$.ajax({ type: "POST", url: "myappengineURL", data: "address=" + sVerifiedEmail, success: function(msg) { alert("Data Saved: " + msg); }, }); 

Assuming I actually know how to use jQuery and invoke that AJAX call...why do I keep getting a 405 Error?

I've re-written this thing six different ways trying to make it work...and I can't! So far I'm looking at advice from http://blog.pythoughts.com/posts/AJAX-with-Google-App-Engine#jqueryAjax and Google code's AJAX RPC article that I can't post a link to because StackOverflow says NO NO NO. Neither of these examples seem to work for me.

What am I doing wrong?

5
  • Can you confirm that the post without javascript works? If it does, is your ajax posting to the same url and passing the same data? Commented Aug 18, 2009 at 7:21
  • 1
    You have an extra comma on line 7 of your jQuery snippet, which may cause the Javascript to fail depending on which browser you're using. Commented Aug 18, 2009 at 7:26
  • Yes, the extra comma was a mistake during my paste. Commented Aug 19, 2009 at 2:47
  • I can confirm that the post works without javascript...I've tried it with everything in page (much like the google example). Commented Aug 19, 2009 at 2:48
  • Is there some kind of security on the App Engine side? Commented Aug 19, 2009 at 3:35

6 Answers 6

5

Your problem is known as "same origin policy". This is why you see the OPTIONS method in your log. The domain and protocol of your Ajax request must be the same as the one you are launching it from.

Here's the same question with good answers.

Sign up to request clarification or add additional context in comments.

Comments

3

I've incorporated jQuery into the Google App Engine AJAX example. Replace their doAdd() and custom AJAX javascript with:

<script language="javascript" src="./static/jquery.js"></script> <script language="javascript" src="./static/json2.js"></script> <script language="javascript"> function doAdd() // Requests server to add two numbers, loads server response to result { $.get( '/rpc', {"action" : "Add", "arg0" : JSON.stringify($("#num1").val()), "arg1" : JSON.stringify($("#num2").val())}, function(response) { $('#result').val(JSON.parse(response)); } ); } </script> 

Works for me! Hope it helps.

Comments

0
$.ajax({ type: "POST", url: "myappengineURL", data: ({address : sVerifiedEmail}), success: function(msg) { alert("Data Saved: " + msg); }, }); 

What happens when you structure your call like I have above?

1 Comment

Same thing...405 method not allowed.
0
  • Check your logs on App Engine. What method is being specified, and what's the URL?
  • Try a POST with Curl or Wget. Does that work?

3 Comments

Doesn't work with a POST. I can't share the URL at the moment...I'll try to set up a dummy site.
This is an odd log item?? 24.251.73.63 - - [17/Aug/2009:22:43:59 -0700] "OPTIONS /add HTTP/1.1" 405 124 - "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2,gzip(gfe)" what's the gzip(gfe) doing in there?
gzip(gfe) is added by the infrastructure. But note the method - 'OPTIONS'. Something you're doing is trying to use the OPTIONS method, which you haven't defined on your class.
0

Instead of: application = webapp.WSGIApplication([('/', EmailList)])

try: application = webapp.WSGIApplication([('.*', EmailList)])

Also, doesn't the data parameter in JS need to be a dictionary? like: var data = {'email': $F('email_field_name')}

Comments

-1

All the other answers were stupid.

You want post instead of get. That should say:

class EmailList(webapp.RequestHandler): def post(self): self.response.out.write("You see nothing!") 

1 Comment

!(This is a brilliant answer! +1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.