我正在学烧瓶。我试图通过jQuery调用获取一个JSON对象到flask中。我的html看起来像这样
<html>
<head>
<title>Passenger</title>
<style type="text/css"></style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div>
<form onsubmit="return false">
<input class="name" type="text" placeholder="Name">
<input class="submit" type="submit" placeholder="Go">
</form>
</div>
<script>
$("input.submit").click(function(e){
$.post( "/save", {name: $("input.name").val(), time: "2pm"});
});
</script>
</body>
</html>
Flask应用程序文件看起来像
^{pr2}$运行此代码将返回None。我希望返回JSON对象[{name:$(”输入.名称“).val(),时间:“2pm”}]。谢谢。在
问题是jquery post没有使用json的数据类型。通过打印出请求内容类型,您可以看到:
将Javascript更改为post json,然后请求.json应该像您预期的那样填充json。在
^{pr2}$请注意,在更改JS代码之后,request.content_类型将是application/json
相关问题 更多 >
编程相关推荐