从Flas中的AngularJS接收JSON

2024-09-26 22:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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错误,如下所示。。。在

^{pr2}$

对于任何好奇的人来说,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
            );

Tags: nameformapijsonhttphostdataapplication
2条回答

您对JSON使用了错误的mimetype。它应该是application/x-www-form-urlencoded,而不是application/x-www-form-urlencoded。在

来自FlaskRequest类文档:

json

If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.

The get_json() method should be used instead.

我觉得你的问题是因为请求.窗体不包含hosts键(包含其他内容),因为mimetype不是json(application/json)。在

要获得更精确的答案,请写出request.form中的内容?在

相关问题 更多 >

    热门问题