簡體   English   中英

帶有Flask的iOS上的JSON POST

[英]JSON POST on iOS with Flask

在我的flask python網絡程序中,我將一些參數保存在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()"> 

服務器端(燒瓶):

@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'

找到了未來讀者的答案:iOS將ajax請求放入緩存。 稍后可能會給出空響應,或者根本不會觸發該函數。 您可以通過在ajax請求中添加一些參數和標頭來解決此問題,這將阻止該請求的發生:

cache: false,
processData: false,
async: true,
headers: {
    "cache-control": "no-cache"
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM