python - "dict object is not callable"尝试从 request.json 获取值时

标签 python json flask

<分区>

我正在使用 JavaScript 获取用户位置并将纬度和经度发送到 Flask 应用程序。但是,当我尝试从 request.json 获取纬度时,我得到了 TypeError: 'dict' object is not callable。为什么会出现此错误以及如何解决?

@app.route('/location', methods = ['POST'])
def location():
    latitude = request.json('latitude')
    longitude = request.json('longitude')
    send_email("myemail@example.com","Location:",str(latitude) + str(longitude))
function showPosition(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    $.ajax({
    type: 'POST',
    url: '/location',
    data: JSON.stringify({latitude: latitude, longitude: longitude}),
    contentType: 'application/json;charset=UTF-8'
    });
}

最佳答案

您需要将 () 替换为 []:

latitude = request.json['latitude'] 

request.json 对象是一个字典,而不是一个函数。

关于python - "dict object is not callable"尝试从 request.json 获取值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31481315/

相关文章:

python - 将 2 列中的值合并为 pandas 数据框中的单列

javascript - 使用 Bootstrap 将 JSON 文件加载到表中

python - Keras - 检查目标时出错

python - 在 plotly 中使用 x 范围 slider 进行 Y 轴自动缩放

html - 如何在 iOS Swift 上从网站(HTML 或 JSON 格式)检索文本和图像?

c++ - 使用boost从json文件中的数组中解析元素

python - flask /夏娃 : Internal GET request with parameters

python - 将 RoleNeed 与 Flask Principal 相结合

python - Firestore Python 中的完成处理

Python NamedTemporaryFile - 读取时出现值错误