0

I am having problem in calling a python function with angularjs $http request. I am having a python function which is on server like this

import cgi, cgitb
data= cgi.FieldStorage()
name = data.getvalue("name");
age = data.getvalue("age");
def printinfo( name, age ):
    print "Name: ", name
    print "Age ", age
    return name,age

and i've also included cgi and my javascript code is

 angular.module('app1',[])
 .controller('ctrl',['$scope','$http' ,function ($scope,$http) {

 $scope.bin = 'examp';
 $scope.go = function () {
        var url = "http://localhost/dump/test/test.py";

   var bad =$http({
            url :url ,
            method:'POST',
            data:{"name":"kumar" , "age":21}
        }).success(function(data){
         alert("working");
       });
    }

 }])

and my javascript code is able to make a call to http://localhost/dump/test/test.py but it is shown as a document even when i included cgi in it .. Please guide me and also can you guys tell me is it the right way to send the values to server ie can i invoke the function print info by just sending name and age or should i send the function name too. If yes let me know how can i pass it ..

Thanks in advance..

1 Answer 1

1

May be your webserver do not know how to handle .py files. You need to configure webserver to handle python. Try the below if it is not configured.

https://www.linux.com/community/blogs/129-servers/757148-configuring-apache2-to-run-python-scripts

But a more good approach is to create a web app using some python framework and expose urls via a web server. If you are interested in that then I would recommend you to learn flask python framework.

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

1 Comment

i've done these changes in my apache config file and created a sample python file used in that example but still it is shown as text in browser.

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.