gpt4 book ai didi

jquery - 是否可以将对象从 jquery 发布到 bottle.py?

转载 作者:行者123 更新时间:2023-11-28 20:50:17 26 4
gpt4 key购买 nike

这里是jquery

$.ajax({
type: "POST",
url: "/posthere",
dataType: "json",
data: {myDict:{'1':'1', '2':'2'}},
success: function(data){
//do code
}
});

这是 python

@route("/posthere", method="POST")
def postResource(myDict):
#do code
return "something"

看起来支持 int、float、path 和 re 的 url 格式......我错过了什么吗?

最佳答案

线上只有字节。要发送一个对象,您需要使用某种数据格式对其进行序列化,例如 json :

$.ajax({
type: "POST",
url: "/posthere",
data: JSON.stringify({myDict: {'1':'1', '2':'2'}}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
alert(data);
},
failure: function(err) {
alert(err);
}
});

在接收端需要将json文本解析为Python对象:

from bottle import request, route

@route("/posthere", method="POST")
def postResource():
#do code
myDict = request.json['myDict']
return {"result": "something"}

返回的字典会自动转为json。

关于jquery - 是否可以将对象从 jquery 发布到 bottle.py?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com