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?

farina
  • 3,486
  • 6
  • 32
  • 44
  • 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? – Andy Gaskell Aug 18 '09 at 07: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. – bryan Aug 18 '09 at 07:26
  • Yes, the extra comma was a mistake during my paste. – farina Aug 19 '09 at 02:47
  • I can confirm that the post works without javascript...I've tried it with everything in page (much like the google example). – farina Aug 19 '09 at 02:48
  • Is there some kind of security on the App Engine side? – farina Aug 19 '09 at 03:35

6 Answers6

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.

Community
  • 1
  • 1
Wolfgang
  • 2,367
  • 23
  • 29
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.

Fraser Harris
  • 1,912
  • 14
  • 19
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?

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
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?
Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • Doesn't work with a POST. I can't share the URL at the moment...I'll try to set up a dummy site. – farina Aug 19 '09 at 03:33
  • 1
    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? – farina Aug 19 '09 at 05:25
  • 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. – Nick Johnson Aug 19 '09 at 08:03
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')}

mahmoud
  • 595
  • 2
  • 6
-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!")
Don
  • 9