在烧瓶应用程序中使用get_json()解码接收到的PUT数据时出错,

时间:2018-08-03 17:16:26

标签: ajax python-2.7 rest flask

我使用flask 0.12,并尝试使用ajax发送PUT请求。但是,使用.get_json()方法时,我收到错误消息无法解码JSON对象。内容为json,is_json返回True。有我的客户端代码:

$.ajax({
    url: apiTechnicians,
    type: 'PUT',
    data: { "technicianUserId": chosenTechnician.val() },
    contentType: "application/json",
    dataType: "json",
    success: function (result) {
        console.log(result)
    }
});

还有服务器端代码:

@api.route("/technicians", methods=['PUT'])
def putTechnicians():
    print request.is_json
    jsonResult = request.get_json()
    return Response(status=200)

有什么建议我应该如何处理接收到的数据以及为什么get_json()无法解码数据?

1 个答案:

答案 0 :(得分:1)

您不是在发送JSON,而是在说它的json(通过内容类型),但不是。
jQuery.ajax将传递给它的对象转换为 Private Sub DetailsButton_Click() Me.Details.Visible = True End Sub 数据,因此要发送json数据,必须将json数据传递给jQuery.ajax

SELECT          user_id,
                cart_id,
                order_total,
                date_abandonment,
                date_abandonment_first,
                fl_abandoned_cart,
                p1_quantity,
                p1_product_name,
                p1_product_description,
                p1_product_type_description,
                p1_product_duration,
                p1_unit_price,
                p2_quantity,
                p2_product_name,
                p2_product_description,
                p2_product_type_description,
                p2_product_duration,
                p2_unit_price,
                p3_quantity,
                p3_product_name,
                p3_product_description,
                p3_product_type_description,
                p3_product_duration,
                p3_unit_price,
                p4_quantity,
                p4_product_name,
                p4_product_description,
                p4_product_type_description,
                p4_product_duration,
                p4_unit_price,
                p5_quantity,
                p5_product_name,
                p5_product_description,
                p5_product_type_description,
                p5_product_duration,
                p5_unit_price
           FROM Cart PIVOT (MAX (quantity) AS quantity,
                                            MAX (product_name) AS product_name,
                                            MAX (product_description) AS product_description,
                                            MAX (product_type_description) AS product_type_description,
                                            MAX (product_duration) AS product_duration,
                                            MAX (unit_price) AS unit_price
                                      FOR (PRODUCT_NUM)
                                      IN  ( (1) AS p1,
                                          (2) AS p2,
                                          (3) AS p3,
                                          (4) AS p4,
                                          (5) AS p5)))