我有一个Python脚本,它在服务器上发送一些事件。代码如下所示:
LOGGER = logging.getLogger("send_event")
POST_EVENT_URL = "http://localhost:3000/event/"
def send(name, data):
url = POST_EVENT_URL + name
headers = {'content-type': 'application/json'}
auth = None
r = requests.post(url, auth=auth, data=json.dumps(data), headers=headers, verify=False)
if (not r.ok) or (200 > r.status_code > 299):
raise IOError(("Failed to upload session to server. OK %s HTTP response code: %d" % (r.ok, r.status_code)))
我需要向这个Python函数传递2个参数,一个是字符串(名称),另一个是json(数据)有人可以帮我弄清楚如何使用AngularJS 1来做到这一点吗?这可能吗?
谢谢!
最佳答案
使用$http
发送http请求。首先将 $http
注入(inject) Controller 并像这样实现
angular.module("app",[])
.controller("ctrl",function($scope,$http){
var POST_EVENT_URL = "http://localhost:3000/event/";
$scope.send = function(name, data){
var url = POST_EVENT_URL + name;
var headers = {'content-type': 'application/json'};
$http({
url : url,
method : "POST",
headers : headers,
data : data
}).then(function(response){
//success
},function(resonse){
//error
})
}
})
关于javascript - Angular + Python post 字符串和 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42647318/