使用angular js客户端对Python服务器进行rest调用的例子

Example on Making rest call to Python server using angular js client

本文关键字:rest 调用 服务器 js angular 客户端 Python 使用      更新时间:2023-09-26

谁能给一个例子,在使用Angularjs从/到python服务器获取和发布数据使用rest。请提供一个客户端(angularjs)和服务器(python)的例子。我的示例客户端代码是

var student_id = 12
$http({
    method:"POST" // or get
    url:       //url to the python file 
    data:     //this is what confusing me .. how to send that student id 
}).success(function(data){
     console.log(data);
   })

在python中,我想运行一个函数打印student id

def print_info(id):   ## here id is the student id that shall be recieved
  print "student id is %s" %id

这里我希望js客户端发送该student_id值到python服务器,它应该接收它,并将print_info函数的值发送到客户端。

你可以这样使用:

$http.post('<your_url>', data).success(function(data) {
    //handle the data returned
}, function(error) {
    //handle if an error is thrown
})

你的数据格式可以是json,像这样:

data = {
    "student_id": value
}