javascript - Angular + Python post 字符串和 json

标签 javascript python angularjs json

我有一个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/

相关文章:

python - Python 中的预计算对象

ios - Cordova/Phonegap 文件传输 iOS 错误 1

javascript - 访问在不同的 .js 文件中定义的 Angular 模块

javascript - 将元数据添加到 AngularJS 资源对象

python - Eclipse中Python的OpenCV 2.3的配置

python - 在 python 中使用 os.walk 更改目录

javascript - 运行 JavaScript 函数 onkeyup 不起作用

javascript - 我如何从 Android 中具有高 javascript 的网站检索?

angularjs - 如何使用angularjs将http POST方法中的两个不同类型的参数传递给Web API?

javascript - 如何在 AngularJS SPA 中实现 SEO