views:

2410

answers:

5

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?

A: 
$.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
Same thing...405 method not allowed.
farina
A: 
  • 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
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
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
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
A: 

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
+2  A: 

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
+4  A: 

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.

Wolfgang