在我的 Flask Python Web 程序中,我在 SessionStorage 中保存了一些参数,以便将它们发送回 Flask,然后将此信息保存为 txt 文件。
出于某种原因,一切在 PC 和 Android 上都能完美运行,但无法在 iOS 设备上运行。它在发送之前识别并保存 sessionStorgae 元素,但之后(在应用程序末尾)不会创建 txt 文件。
客户端(HTML):
function func()
{
...
$.ajax({
url: "{{ url_for('getInfo', _external=True) }}",
type: 'POST',
dataType: 'json',
contentType: 'application/json;charset=UTF-8',
success: function (response) {
console.log(response);
},
accepts: {
json: 'application/json',
},
data: JSON.stringify(log)
});
return;
...
}
<form id = "myForm" name ="myForm" method="post" action="{{url_for('final')}}" onsubmit="return func()">
服务器端(Flask):
@app.route("/get_info",methods = ['POST'])
def getInfo():
list = request.get_json()
global id
with open(id + '.txt', 'w+') as outfile:
json.dump(list,outfile,indent=2)
return 'OK'
我无法找出解决方案。我也不记得它是否曾经在 iOS 上运行过。在我写这篇文章时尝试运行各种测试。任何帮助将非常感激。谢谢。
如果您认为需要更多信息,我在评论中详细说明了该 HTML 页面和 Web 程序的整体结构。
最佳答案
您的代码处于“调试”模式
@app.route("/get_info",methods = ['POST'])
def getInfo():
list = request.get_json()
global id
file_name = '{}.txt'.format(id)
print('About to write {} to file {}'.format(str(list),file_name))
with open(file_name, 'w+') as outfile:
json.dump(list,outfile,indent=2)
print('Data was written')
return 'OK'
关于javascript - 使用 Flask 在 iOS 上进行 JSON POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090319/