我在Python中创建一个JSON对象,然后将其发送到JavaScript文件以在网站上显示该值。
我已经尝试使用单引号,双引号之间的单引号
InfosThymio = {"proxh": "N/A"}
然后我发送:
def getInfos():
global infosThymio
return str(infosThymio)
我的问题是JavaScript得到这样的:
{'proxh':'N/A'}
和我得到的unexpected token ' at position 1
在JavaScript中,我使用这种方式来获取JSON并对其进行解析:
getRobotData() {
api.get()
.then((data) => data.json())
.then((res) => {
const data = JSON.parse(res);
this.setCapteur('hProxi', data.proxh);
您不需要编写,const data = JSON.parse(res);
因为JSON已经用该行进行了解析.then((data) => data.json())
该行.then((data) => data.json())
告诉计算机从您的API解析JSON响应并返回生成的javascript对象。
这样命名变量在语义上会更正确:
getRobotData() {
api.get()
.then((res) => res.json()) // The JSON response is parsed into data
.then((data) => {
// Do something with your data
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句