1

I am newbie in python and am using Python to get the request from AJAX calls but I am getting the code of python as it is inside the AJAX output which I don't want.

AJAX Code :

$.ajax({
        type: "POST",
        url: "hello.py",
        datatype : "json",
        data: { name: "nitin"}
      })
      .done(function(msg) {
           console.log(msg);
           $("div").html("Data : "+msg)
});

Python Code :

import sys
import cgi

sys.stdout.write("Content-Type: application/json")
sys.stdout.write("\n") 
sys.stdout.write("\n")

form = cgi.FieldStorage()

sys.stdout.write(json.dumps({ 'data': form.getvalue('name')}))

Output : Data : import sys import cgi sys.stdout.write("Content-Type: application/json") sys.stdout.write("\n") sys.stdout.write("\n") form = cgi.FieldStorage() sys.stdout.write(json.dumps({ 'data': form.getvalue('name')}))

Expected Output should be :

Data : Content-Type: application/json

nitin
3
  • Have you configured your web server to treat Python fires as cgi? And why are you using cgi in the first place? Commented Oct 12, 2014 at 13:36
  • Sorry sir.. I don't have any idea configure web server to treat Python fires as cgi. Didn't get why are you using cgi in the first place?. Commented Oct 12, 2014 at 13:46
  • I have added Options +ExecCGI AddHandler cgi-script .py these line in .htaccess file in my local web server(apache). After adding this, python script is now executable but unable to get response of python in the AJAX. Commented Oct 12, 2014 at 14:41

2 Answers 2

1

is your python script marked as executable? see this -

"The Python script is not marked as executable. When CGI scripts are not executable most web servers will let the user download it, instead of running it and sending the output to the user. For CGI scripts to run properly on Unix-like operating systems, the +x bit needs to be set. Using chmod a+x your_script.py may solve this problem."

there is also a sample to test if your server supports CGI -

https://docs.python.org/2/howto/webservers.html#simple-script-for-testing-cgi

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

2 Comments

yes. my python script is marked as executable. Before running it, I had done chmod +x /path-to-localhost/hello.py. Any other help. plz
I have added Options +ExecCGI AddHandler cgi-script .py these line in .htaccess file in my local web server(apache). After adding this, python script is now executable but unable to get response of python in the AJAX.
0

Add below 2 lines inside .htaccess file under /home/*/public_html.

Options +ExecCGI 
AddHandler cgi-script .py

Python code should be like that :

import sys
import cgi
sys.stdout.write("Content-Type: application/x-www-form-urlencoded")
sys.stdout.write("\n") 
sys.stdout.write("\n")
form = cgi.FieldStorage()
name = form.getvalue('name')
print name

Because content-type is of application/x-www-form-urlencoded not application/json. We are getting form values. If want to see the type of content simply print form.headers.

Comments

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.