In a very basic test web app I am making, I am using angular to run a function when a form is submitted. The function asynchronously posts data to a simple api I built which is supposed to input data into a database dependent on the POST information it receives. It seems like the POST is working correctly on the front end, however I cannot access request.json
from Flask at all or get any of the post data. I feel like this problem may be something simple I have overlooked but as of now I cannot figure it out at all. Here is some of the code:
在我正在开发的一个非常基本的测试web应用程序中,我在提交表单时使用角来运行函数。该函数异步地将数据发布到我构建的一个简单api中,该api应该根据所接收的POST信息将数据输入到数据库中。看起来这篇文章在前端的工作是正确的,但是我不能访问请求。来自Flask的json或获得任何post数据。我觉得这个问题可能是我忽略了的一个简单的问题,但到目前为止我完全无法解决它。以下是一些守则:
AngularJS:
AngularJS:
$scope.submitAddMsg = function (){
var data = {'author': $scope.msgauthor, 'message': $scope.msgmsg};
$http.post('/addMessage', data, {headers: {'Content-Type': 'application/json'}}).
success(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
}).
error(function(data, status, headers, config) {
alert(JSON.parse(data));
});
};
Flask view function for /addMessage
Flask视图函数
@app.route('/addMessage', methods=['POST'])
def addMessage():
#results in 'None type not iterable'
#response = request.form.get('author')
#results in a 400 error
#response = request.get_json()
#results in a 400 error
#response = request.get_json()
#results in 'author' not defined
#name = request.args.get('author')
#return jsonify(author = author)
return str(jsonify(response))
I cannot stop getting errors as if the request is not what I think it should be, is there something else I should be doing to properly handle this? Because I cannot access any POST information when using Angular to send the POST or even a REST Client with payload exactly how the angular is sending data.
我不能停止错误,好像请求不是我认为应该的那样,我是否应该做些什么来正确地处理这个问题?因为我不能访问任何POST信息,当使用角来发送POST时,甚至是使用负载的REST客户端,也不能访问角是如何发送数据的。
Here is the JavaScript console to see what data
, status
, headers
, and config
ends up being in the success function that runs after the POST:
这里是JavaScript控制台,查看哪些数据、状态、头和配置最终会出现在运行后的成功函数中:
<Response 46 bytes [200 OK]>
testbase.js:53 200
testbase.js:54 function (c){a||(a=Xc(b));return c?(c=a[z(c)],void 0===c&& (c=null),c):a}
testbase.js:55 Object {method: "POST", transformRequest: Array[1], transformResponse: Array[1], headers: Object, url: "/addMessage"…}data: Objectheaders: ObjectAccept: "application/json, text/plain, */*"Content-Type: "application/json"__proto__: Objectmethod: "POST"transformRequest: Array[1]transformResponse: Array[1]url: "/addMessage"__proto__: Object
Any help on getting this working right is much appreciated, let me know if you need more information
如果您需要更多的信息,请告诉我
2 个解决方案
#1
0
you can use request.data
to get the raw post data.
您可以使用请求。获取原始post数据的数据。
you can also set the silent
Flag of get_json
to True
so you can get the exact message of failure.
您还可以将get_json的静默标志设置为True,这样就可以得到失败的确切消息。
from the docs
从文档
get_json(force=False, silent=False, cache=True)
get_json(力= False,沉默= False,缓存= True)
Parameters:
force – if set to True the mimetype is ignored.
silent – if set to False this method will fail silently and return False.
cache – if set to True the parsed JSON data is remembered on the request.参数:force -如果设置为True, mimetype将被忽略。如果设置为False,该方法将静默失败并返回False。缓存——如果设置为True,则会在请求中记住解析过的JSON数据。
#2
0
Try adding app.debug = True
before you start the app and try again.
在启动应用程序之前,尝试添加app.debug = True,然后再试一次。
You can also try: message = request.json.get('message')
您也可以尝试:message = request.json.get('message')
And I would also try putting this in your route
我也会尝试把这个放在你的路线上
['POST', 'OPTIONS'])
['文章','选项'])
#1
0
you can use request.data
to get the raw post data.
您可以使用请求。获取原始post数据的数据。
you can also set the silent
Flag of get_json
to True
so you can get the exact message of failure.
您还可以将get_json的静默标志设置为True,这样就可以得到失败的确切消息。
from the docs
从文档
get_json(force=False, silent=False, cache=True)
get_json(力= False,沉默= False,缓存= True)
Parameters:
force – if set to True the mimetype is ignored.
silent – if set to False this method will fail silently and return False.
cache – if set to True the parsed JSON data is remembered on the request.参数:force -如果设置为True, mimetype将被忽略。如果设置为False,该方法将静默失败并返回False。缓存——如果设置为True,则会在请求中记住解析过的JSON数据。
#2
0
Try adding app.debug = True
before you start the app and try again.
在启动应用程序之前,尝试添加app.debug = True,然后再试一次。
You can also try: message = request.json.get('message')
您也可以尝试:message = request.json.get('message')
And I would also try putting this in your route
我也会尝试把这个放在你的路线上
['POST', 'OPTIONS'])
['文章','选项'])