我有一个AngularJS应用程序,它正在将HTTP PUT
数据发送到flask;我可以看到数据正确到达服务器。。。但是,由于某些原因,当Angular向服务器发出HTTP PUT
时,我的flask方法无法读取它。。。在
烧瓶代码:
@app.route('/api/thing/hostform', methods=['GET'])
@login_required
def get_blank_host_row():
retval = [host_row(host_name="RANDOM_HOST")]
return Response(dumps(retval), mimetype='application/json')
@app.route('/api/thing/hostform', methods=['PUT'])
@login_required
def append_blank_host_row():
retval = request.form.get('hosts', "!! ERROR !!")
print "PUT RESULT", retval
retval.append(host_row())
return Response(dumps(retval), mimetype='application/json')
“formsubmit_add2”正确地来自/api/thing/hostform
;但是,由于某些原因,request.form.get('hosts', "!! ERROR !!")
总是出现一个HTTP 500错误,如下所示。。。在
对于任何好奇的人来说,request.json
就是{HTTP PUT
。。。在
如何正确接收AngularJS发送给flask的内容?在
HTTP PUT的Wireshark转储:
这是来自AngularJS的httpput的wireshark转储。。。在
Hypertext Transfer Protocol
PUT /api/thing/hostform HTTP/1.1\r\n
[Expert Info (Chat/Sequence): PUT /api/thing/hostform HTTP/1.1\r\n]
[Message: PUT /api/thing/hostform HTTP/1.1\r\n]
[Severity level: Chat]
[Group: Sequence]
Request Method: PUT
Request URI: /api/thing/hostform
Request Version: HTTP/1.1
Host: tsunami:5000\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:28.0) Gecko/20100101 Firefox/28.0\r\n
Accept: application/json, text/plain, */*\r\n
Accept-Language: en-US,en;q=0.5\r\n
Accept-Encoding: gzip, deflate\r\n
Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n
Referer: http://server_name:5000/formsubmit_add2\r\n
Content-Length: 164\r\n
[Content length: 164]
Connection: keep-alive\r\n
\r\n
[Full request URI: http://server_name:5000/api/thing/hostform]
Line-based text data: application/x-www-form-urlencoded
{"hosts":[{"real_ip_addr":"","switch_port":"","host_nic_role":"Console",
"host_name":"RANDOM_HOST","host_nic_name":"","switch_name":"",
"altn_ip_addr":"192.0.2.42"}]}
AngularJS$http PUT:
$scope.add_row = function (data) {
// build an ajax request with $scope.data
var send = $http({
method : "PUT",
url : "/api/thing/hostform",
headers : {"Content-Type":
"application/x-www-form-urlencoded; charset=UTF-8"},
data: {"hosts": data},
});
send.success(
// something irrelevant here
);
您对JSON使用了错误的mimetype。它应该是
application/x-www-form-urlencoded
,而不是application/x-www-form-urlencoded
。在来自Flask
Request
类文档:我觉得你的问题是因为请求.窗体不包含
hosts
键(包含其他内容),因为mimetype不是json(application/json)。在要获得更精确的答案,请写出
request.form
中的内容?在相关问题 更多 >
编程相关推荐