Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

python - How to close webview and get json response in flask?

I want to close my selectGroupView after getting response when I click on submit button. Currently I am not getting any response as my selectGroupView is closing suddenly whenever I click on submit button.

Actually I want to return a json data after submission of a form. So that I can use it further. Please Help me.

app.py

from flask import Flask, request,render_template

@app.route('/select-group/<string:assignerID>',methods=['GET'])
def selectGroupView(assignerID):
    assignerInfo = info.assignerInfo(assignerID,[])
        
    assignerID = assignerInfo['id']
    assignerName = assignerInfo['name']
    assignerGroups = assignerInfo['groups']

    return render_template('groups.html',groups = assignerGroups, assignerID = assignerID, assignerName=assignerName)

@app.route('/setup-group',methods=['POST'])
def setupGroupView():

    assignerID = request.form.get('assignerID')
    assignerName = request.form.get("assignerName")

    selectedGroup = request.form.get("group")
    selectedGroupID,selectedGroupName = selectedGroup.split(",")
    
    bot.sendTextMessage(assignerID,f'*{selectedGroupName}* Group Selected Successfully.')
    bot.sendTextMessage(assignerID,f'Please Wait')

    return '<h1>'I want to return Json'<h1>',200

groups.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Select Group</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='styles/style.css') }}">
</head>
<script>
    (function(d, s, id){
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/messenger.Extensions.js";
            fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'Messenger'));
    
    function handleSaveBtn(){
            MessengerExtensions.requestCloseBrowser(function success() {
          // webview closed
        }, function error(err) {
            console.log(err);
          // an error occurred
        });
        }

</script>
<body>
    <div class='head'>
        <img src="{{ url_for('static',filename='Image/logo.webp') }}" alt="Italian Trulli">
    </div>

    <div class="form">
        <form method="POST" action="/setup-group">
            <input type="text" hidden name="assignerID" id="psid" value="{{assignerID}}">
            <input type="text" hidden name="assignerName" id="psname" value="{{assignerName}}">
            {% for i in groups %}
            <input type="radio" id="{{i.group_name}}" name="group" value="{{i.group_id}},{{i.group_name}}">
            <label for="{{i.group_name}}"><b>{{i.group_name}}</b></label><br><br>
            {% endfor %}
        </br>
            <button type="submit" onclick="handleSaveBtn()">Save</button>
          </form>
    </div>
    
</body>
</html>
question from:https://stackoverflow.com/questions/65844816/how-to-close-webview-and-get-json-response-in-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe your problem is likely to with how you've configured your request processes and the closing of your webview.

Your post request

<form method="POST" action="/setup-group">

is made to the /setup-group endpoint; the relevant function will process the request and return the JSON response that you require. I don't see anything wrong here.

For your onClick function that handles closing your web view may interfere and likely causing your problem.

<button type="submit" onclick="handleSaveBtn()">Save</button>

It is likely that before the JSON response is received your webview is closed and thus voiding any returned response from the backend. You can try to add a sleep call in the JavsScript function to delay the closure, or you can also configure a callback to wait until a response is received before closing the webview - see the example here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...