简体   繁体   中英

Calling python function in server from local machine using angularjs

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM