0

I am using Google App Engine to develop an application, and I want to use an AJAX call to execute Python script. I can retrieve the script, but I cannot execute it.

I would use WSGI, as suggested here: How do you execute a server-side Python script using jQuery?. However, I do not want my page to redirect, and my solution to keeping to page from redirecting was to use an AJAX call as suggested here: Run CGI application without redirecting from HTML, and here: Make a tag act like input button.

Right now I use an HTML form but I want to stop it from redirecting the page.

<form action="/token" method="post" >
 <div> "some button code" </div>
 </form>

So I have been trying to use AJAX instead, but it returns the code without executing it

$.post('js/mailToken.py', { testdata: 'hello world' },alert("success"));
5
  • 2
    If the ajax request is receiving the source code of the script rather then the output of the script you need to look in to your server configuration, not your AJAX code. Commented Aug 17, 2012 at 20:10
  • Thanks, I was afraid of that, because that would involve configuring Google App Engine. Commented Aug 17, 2012 at 20:33
  • Yes! Is that where I would configure the server to return the output of a Python script rather than the script itself? Commented Aug 17, 2012 at 20:55
  • I'm not 100% sure, as I've never used GAE, but from the Hello, World example, it looks like you configure it there? Commented Aug 17, 2012 at 20:59
  • you should post your app.yaml in you want an help Commented Aug 18, 2012 at 12:10

1 Answer 1

2

You're trying to seervve up a python script as a static file, that's why app engine is returning the code without executing it.

Assuming you're coming from the world of PHP, where, unless you're doing URL rewriting in .htacces (on apache server), pointing your browser to the location of a PHP script will execute that script.

In App Engine, and many python web frameworks, this works differently. In order to point a certain URI tyo a certain script, you edit the app.yaml file in your app directory.

by default, all URI's will point to a script called main.py. This is defined in the app.yaml that's generated when you create your app using google app engine launcher. The part where this is defined looks like this:

handlers:
- url: /
  script: main.py

within main.py, you'll want to further specify how your requests are handled based on the URI. You'll probably want to do this using the webapp or webapp2 frameworks, based on wether you use python 2.5 or 2.7. Once that's done, you can simple send requests to these URI's no matter wether it's through AJAX, or as the action of a form, or just by going directly to it, or clicking a link.

Explaining the entire webapp and webapp2 frameworks in order to understand how to further handle requests, is probably beond the scope of this answer (read: I'm getting tired of typing :p), but the app engine documentation has great "getting started" guides for python 2.5 and python 2.7.

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

1 Comment

Thanks for the response. It turns out the issue was with my AJAX call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.