You need to enable JavaScript to run this app. 简易Flask应用出现Connection Reset问题,寻求技术解决方案
优惠活动
大模型
产品
解决方案
定价
更多

简易Flask应用出现Connection Reset问题,寻求技术解决方案

Hey there, let's work through that connection reset issue in your Flask + AJAX app. I see a few potential fixes based on the code you shared:

1. Fix Incomplete AJAX Code

Your AJAX snippet cuts off at aler...—that's a typo (should be alert) and you're missing the actual request logic to hit your Flask route. An incomplete or broken AJAX call can easily cause unexpected connection drops. Here's a full, working version:

$('.check-grammer').click(function(e) {
    e.preventDefault();
    const text = $('#exampleTextarea').val();
    if (text === '') {
        alert('Please enter some text first!');
        return;
    }

    $.ajax({
        url: '/check_grammer',
        type: 'POST',
        data: { text: text },
        success: function(response) {
            console.log('Got response:', response);
            // Add your logic to display grammar check results here
        },
        error: function(xhr, status, error) {
            console.error('Request failed:', error);
            alert('Oops, something went wrong checking grammar.');
        }
    });
});

2. Improve Flask Route Handling

Right now your /check_grammer route only returns the request method, which doesn't give the frontend useful data. Let's adjust it to handle POST data properly and return a JSON response (AJAX works much smoother with JSON than plain text):

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template('smart_writer.html')

@app.route('/check_grammer', methods=["GET", "POST"])
def check_grammer():
    if request.method == 'POST':
        input_text = request.form.get('text', '')
        # Replace this mock logic with your actual grammar checking code
        mock_result = {
            'original_text': input_text,
            'corrected_text': input_text.replace('grammer', 'grammar'),
            'issues': ['Spelling: "grammer" should be "grammar"'] if 'grammer' in input_text else []
        }
        return jsonify(mock_result)
    return jsonify({'message': 'Use POST to submit text for grammar check'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

3. Rule Out Network/Firewall Issues

  • If testing locally, try accessing http://localhost:5000 instead of the 0.0.0.0 IP—sometimes binding to all interfaces can cause minor connection quirks.
  • Double-check that port 5000 isn't blocked by your firewall or being used by another application.
  • Temporarily disable debug mode (debug=False)—the auto-reloader in debug mode can occasionally trigger connection resets when the app reloads.

4. Verify Template Setup

Make sure your smart_writer.html has all the required elements:

  • Include jQuery before your script (since you're using $ for AJAX):
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
  • Confirm your button has the class check-grammer and your textarea has the ID exampleTextarea—mismatched selectors will break the AJAX trigger entirely.

Give these fixes a shot, and the connection reset issue should clear up!

内容的提问来源于stack exchange,提问作者Imesh Chandrasiri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:51:09