我使用flask 0.12,并尝试使用ajax发送PUT请求。但是,我收到一个错误,在使用方法时无法解码JSON对象.get_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()
不能对数据进行解码?
您不是在发送JSON,而是说它的json(通过内容类型),但不是。
jQuery.ajax将传递给它的对象转换为application/x-www-form-urlencoded
数据,因此要发送json数据,您必须将json数据传递给jQuery.ajax
$.ajax({
url: apiTechnicians,
type: 'PUT',
data: JSON.stringify({ "technicianUserId": chosenTechnician.val() }),
contentType: "application/json",
success: function (result) {
console.log(result)
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句