Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
AJAX showing a json file which i returned instead of updating the data in the page

I'm using AJAX to send a user-input form to Flask, where it is processed, used in a query, and the results are sent back as a JSON object. AJAX is called upon form submission, and I'm returning the proper query results. But, when the results are returned, the JSON object is printing to the browser window, not maintaining the template format of the original page.

I expect the returned JSON object to be printed to the screen in the p tag with id="result", but instead, the data returned is printed directly to the browser window,

AJAX Script

<script type=text/javascript>
        $(function() {
          $('button#submit').bind('click', function() {
            $.getJSON('/index', {
              text: $('textarea[name="text"]').val(),
            },
            sucess: function(data) {
              $('#result').text(JSON.stringify(data.result));
            });
            return false;
          });
        });

HTML

<div class="col ">
            <form action="/" method="POST">
                <div class="form-group shadow p-3 bg-white rounded">
                    <!-- <label for="content">Content</label> -->
                    <textarea class="form-control bg-light" id="text" name="text"
                              placeholder="Copy and paste the text of the URL of the article"
                              rows="13"
                              style="resize: none"></textarea>
                    <a href="#" id="submit">
                        <button class="btn btn-danger text-white mt-2" type="submit">Summarize
                        </button>
                    </a>

                </div>
            </form>
        </div>
        <div class="col">
            <div class="col shadow p-3 h-100 bg-white rounded">
                <p id="result"></p>
            </div>
        </div>

FLASK APP

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        text = request.form.get("text")
        if text:
            return jsonify(result=text)
        else:
            return jsonify(result="input needed")
    return render_template("index.html")

View the sample response here

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda