Python 将数据与GET请求一起发送到flask API

Python 将数据与GET请求一起发送到flask API,python,angularjs,rest,http,flask,Python,Angularjs,Rest,Http,Flask,我正在尝试将我在Python Flask中开发的API与AngularJS API结合起来: </head> <body> </body> <div ng-app="myApp" ng-controller="myCtrl"> <h1>{{myWelcome}}</h1> </div> <script> var app = angular.module('myApp', []); app.contro

我正在尝试将我在Python Flask中开发的API与AngularJS API结合起来:

</head>
<body>
</body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
        var json = {
            what:  JSON.stringify(" is going on?")
        };
         try {
             $req = $http({
                        method: 'get',
                        url: "http://localhost:9999",
                        data: JSON.stringify(json),
                        //Data: JSON.stringify(json),
                        headers: {
                            'Content-Type': 'application/'
                        }
                    }).success(function (data) {
                        console.log('Received data via HTTP from ', [data]);
                    }).error(function (data, status) {
                        console.log("Error receiving from HTTP");
                    });
                } catch (err) {
                    console.log( null, "EXCEPTION: " + err.message);
                }
</script>

</html>
运行这个服务器(python file.py-serve将提供服务,python file.py-post将在服务中提供一个json)我使一切正常工作,我能够发送数据并将其接收回来

from flask import Flask, request, jsonify, send_from_directory
from json import dumps, loads
app = Flask(__name__, static_folder=".", template_folder=".")

@app.after_request
def add_headers(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization, data')
    return response


@app.route("/")
def main():
    content = loads(request.data)
    return jsonify({"status":"ok", 'content':content})


@app.route("/index.html")
def index():
    return send_from_directory(".", "index.html")


if __name__ == '__main__':
    import sys
    if sys.argv[1] == "serve":
        app.run(host="0.0.0.0", debug=True, port=9999)
    elif sys.argv[1] == "post":
        import requests
        ans = requests.get("http://localhost:9999/", data=dumps({"what":"is going on?"}))
        print ans.text
</head>
<body>
</body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
        var json = {
            what:  JSON.stringify(" is going on?")
        };
         try {
             $req = $http({
                        method: 'get',
                        url: "http://localhost:9999",
                        data: JSON.stringify(json),
                        //Data: JSON.stringify(json),
                        headers: {
                            'Content-Type': 'application/'
                        }
                    }).success(function (data) {
                        console.log('Received data via HTTP from ', [data]);
                    }).error(function (data, status) {
                        console.log("Error receiving from HTTP");
                    });
                } catch (err) {
                    console.log( null, "EXCEPTION: " + err.message);
                }
</script>

</html>
但是当我使用这个简单的Angular应用程序时,服务器崩溃,因为那里的request.data不是有效的JSON,我尝试将信息作为URL参数发送到HTTP数据头中,也没有成功。

</head>
<body>
</body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
        var json = {
            what:  JSON.stringify(" is going on?")
        };
         try {
             $req = $http({
                        method: 'get',
                        url: "http://localhost:9999",
                        data: JSON.stringify(json),
                        //Data: JSON.stringify(json),
                        headers: {
                            'Content-Type': 'application/'
                        }
                    }).success(function (data) {
                        console.log('Received data via HTTP from ', [data]);
                    }).error(function (data, status) {
                        console.log("Error receiving from HTTP");
                    });
                } catch (err) {
                    console.log( null, "EXCEPTION: " + err.message);
                }
</script>

</html>

{{myWelcome}}
var-app=angular.module('myApp',[]);
app.controller('myCtrl',函数($scope,$http){
var json={
什么:JSON.stringify(“正在进行吗?”)
};
试一试{
$req=$http({
方法:“get”,
url:“http://localhost:9999",
数据:JSON.stringify(JSON),
//数据:JSON.stringify(JSON),
标题:{
“内容类型”:“应用程序/”
}
}).成功(功能(数据){
log('通过HTTP从',[data]接收数据]);
}).错误(功能(数据、状态){
log(“从HTTP接收错误”);
});
}捕捉(错误){
日志(null,“异常:+err.message”);
}
有关如何执行此操作的任何示例?

如果您确实希望通过GET请求传递数据,可以通过请求参数执行此操作:

</head>
<body>
</body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
        var json = {
            what:  JSON.stringify(" is going on?")
        };
         try {
             $req = $http({
                        method: 'get',
                        url: "http://localhost:9999",
                        data: JSON.stringify(json),
                        //Data: JSON.stringify(json),
                        headers: {
                            'Content-Type': 'application/'
                        }
                    }).success(function (data) {
                        console.log('Received data via HTTP from ', [data]);
                    }).error(function (data, status) {
                        console.log("Error receiving from HTTP");
                    });
                } catch (err) {
                    console.log( null, "EXCEPTION: " + err.message);
                }
</script>

</html>
js:

</head>
<body>
</body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
        var json = {
            what:  JSON.stringify(" is going on?")
        };
         try {
             $req = $http({
                        method: 'get',
                        url: "http://localhost:9999",
                        data: JSON.stringify(json),
                        //Data: JSON.stringify(json),
                        headers: {
                            'Content-Type': 'application/'
                        }
                    }).success(function (data) {
                        console.log('Received data via HTTP from ', [data]);
                    }).error(function (data, status) {
                        console.log("Error receiving from HTTP");
                    });
                } catch (err) {
                    console.log( null, "EXCEPTION: " + err.message);
                }
</script>

</html>
烧瓶:

</head>
<body>
</body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{myWelcome}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope, $http) {
        var json = {
            what:  JSON.stringify(" is going on?")
        };
         try {
             $req = $http({
                        method: 'get',
                        url: "http://localhost:9999",
                        data: JSON.stringify(json),
                        //Data: JSON.stringify(json),
                        headers: {
                            'Content-Type': 'application/'
                        }
                    }).success(function (data) {
                        console.log('Received data via HTTP from ', [data]);
                    }).error(function (data, status) {
                        console.log("Error receiving from HTTP");
                    });
                } catch (err) {
                    console.log( null, "EXCEPTION: " + err.message);
                }
</script>

</html>
@app.route("/")
def main():
    content = loads(request.args['data'])
    return jsonify({"status":"ok", 'content':content})

HTTP GET没有请求主体。为什么不使用Flask POST端点?如果没有,我的GET是如何使用请求在Python中编写的?我知道从HTTP/REST的角度来看,它在语义上可能不正确,但有一种方法可以在angular上执行等效的请求?