Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

javascript - How do you execute a server-side Python script using jQuery?

I have a very simple Python file, called python1.py, whose contents are:

f = open('C:\Temp\test.txt', 'w')
f.write('Succeeded')
f.close()

I wish to execute this from JavaScript, like so:

jQuery.ajax({
   type: "POST",
   url: "/cgi-bin/python1.py",
   success: function (msg) {
       alert("Data Saved: " + msg);
   }
});

However, all that happens is that I get an alert showing me the contents of the Python script. The file C:Tempest.txt does not get created, so clearly the Python was not executed.

How do I persuade the code to execute the Python script instead of just reading it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You simply need to configure your web server to execute your *.py scripts, instead of serving them as plain text.

If you are using Apache as a web server, you need to enable mod_python or mod_wsgi.


EDIT:

Since you are using using Apache, you may want to check the following article, which briefly describes how to set up the mod_python module:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...