2

I have seen this topic: ajax request to python script where they were trying to do exactly what I need but there is one information missing.

$.post('myPythonFile.py',{data}, function(result){ //TODO } );

Now my problem is: how do I call a certain function which is inside myPythonFile.py? Is there a way to specify the name of the function and to give it my input data? Thank you very much for your time.

Community
  • 1
  • 1
user2343163
  • 23
  • 1
  • 3
  • You probably can't call the function directly, but you could have a value in the posted data that you do an if-else on inside the python code. – Stian May 02 '13 at 13:00
  • So the python file has to be a function itself instead of containing functions. Is that correct? – user2343163 May 02 '13 at 13:09
  • I see you are using Django, I would advice you to read the documentation to understand how frameworks like that work.. – Stian May 02 '13 at 13:15
  • There is no in-built, straight out-of-the-box support for Python.. It really depends on how your server is configured and what you are running on it. Therefore it's difficult to answer this question unless you specify which framework/server software you use. – MMM May 02 '13 at 13:18
  • First send the function name you need to django. The name can be plain text. Then you can call `globals()` in python, it will return a dict which stores all name in your current module. For example, you want to call `Myfunction`: send text `{text:"Myfunction"}` in json form, then in your py call `globals()[request.text]()`. it should be called. – Herrington Darkholme May 02 '13 at 13:51
  • By the way, I don't think calling a certain function from ajax will be a good idea. If malicious request send request like `import os`, it will be a serious security issue. Much effort will be needed to prevent attacks. – Herrington Darkholme May 02 '13 at 13:52
  • Thanks to all of you guys. I think the best way will be follow the path suggested in the answer below and build an api to serve that ajax request...sounds neat and clean. – user2343163 May 02 '13 at 14:01

1 Answers1

0

Ajax calls making HTTP requests,so you need to have a HTTP server which handles your requests as it is seen in the other question. (There is CGI which provide HTTP request handling). In Python you can use DJango, Bottle, CGI etc to have HTTP request handling. to call python function from javascript.

Edited :

in your url.py you should define your api url;

(r'^myAPI', 'myAPI'),

and on this url you should have a web API in views.py. It can be like ;

def myAPI(request):
    callYourFunction();

and you can call it from Javascript now. You can use JQuery for AJAX request;

 $.ajax({
            type:"GET",
            url:"/myAPI",
            contentType:"application/json; charset=utf-8",
            success:function (data) {     
            },
            failure:function (errMsg) {
            }
        });

The HTTP method type does not matter, if you only wanna run a Python script. If you wanna send data from javascript to Python you can send the data as JSON to Python as POST method.

Community
  • 1
  • 1
Ahmet DAL
  • 4,445
  • 9
  • 47
  • 71
  • I am using Django but I am not really familiar with it. Should I write something in the url.py file? Sorry but I'm new to this kind of stuff. – user2343163 May 02 '13 at 13:10
  • Thanks Ahmet DAL. I will work on this path but it will take me sometime to figure out how things work. – user2343163 May 02 '13 at 13:27